Python sql statement error

I had 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 followed the this link as a link.

The error I am getting is:

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

I can't figure out which part of the syntax is incorrect here?

+4
source share
1 answer

Please write the following line

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

as below:

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

or combine two lines. As now, there is a syntax error.

+3
source

Source: https://habr.com/ru/post/1610077/


All Articles