You have an error in your SQL syntax error appearing with correct syntax

Question:

I am making an app in python flask, and I executed a sql query. I am using Mysql server 8.0.
My code is:

    mydb = mysql.connector.connect(
        host="localhost",
        user="...",
        password=".....",
        database="....."
    )
    cursor = mydb.cursor()
    sql = "INSERT INTO calender_events (class,date,title,desc) VALUES (%s, %s ,%s, %s)"
    val = (str(student_class), str(date.today()),title,desc)
    cursor.execute(sql, val)
    mydb.commit()

I get the error:

mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc) VALUES ('8a', '2023-01-23' ,'er', 'er')' at line 1

although my syntax is correct, I think. I do not know my this error is occuring. Any help would be greatly appreciated. Thanks!

Asked By: Prateek p

||

Answers:

This is because desc is a reserved word in MySQL. See this question, which shows you should use

sql = "INSERT INTO calender_events (class,date,title,`desc`) VALUES (%s, %s ,%s, %s)"

note the backticks around desc. Alternatively, you could use a different name for this column, maybe description?

Answered By: ACarter
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.