4

I've run into a problem while trying to execute an insert statement from python.

Here is my function definition:

def fill_course(param_string):
    ar = param_string.split("|")
    db = connect()
    sql = (
        "INSERT INTO COURSE(`NAME`, `DURATION`, `DEPT`) "
        "VALUES (%s, %s, %s)"
    )
    data = ar[0], ar[1], ar[2]
    cursor = db.cursor()
    cursor.execute(sql, data)
    db.commit()
    if cursor.rowcount == 0:
        res = 0
    elif cursor.rowcount == 1:
        res = 1
    db.close()
    print(res)
    return res

I've followed this link as a reference.

The error I am getting is :

 File "database.py", line 25
    "INSERT INTO COURSE        "VALUES (%s, %s, %s)"
                                     ^
SyntaxError: invalid syntax

I am not able to understand which part of the syntax is wrong here?

0

1 Answer 1

3

Please write the following string

"INSERT INTO COURSE(`NAME`, `DURATION`, `DEPT`) "
    "VALUES (%s, %s, %s)"

like below:

"INSERT INTO COURSE(`NAME`, `DURATION`, `DEPT`) VALUES (%s, %s, %s)"

or concatenate the two strings. As it is now, there is a syntax error.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanx! But can't understand why is it given like that in the mysql python documentation!!
You are welcome ! I am glad that I helped. Everyone can mistype something, even the writer or writers of a documentation :)
Depends where you look I guess - cur.execute("INSERT INTO Writers(Name) VALUES('Jack London')") - zetcode.com/db/mysqlpython

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.