0

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!

Prateek p
  • 117
  • 1
  • 11
  • 1
    Did you try to check the contents of `val` before preparing the query? Are they as you expect? Did you try to check what the prepared query will look like? Does that look right? – Karl Knechtel Jan 23 '23 at 15:30
  • 1
    @RiggsFolly no, they match up properly (and if that were the problem, it would be reported as a Python syntax error, not an SQL one). – Karl Knechtel Jan 23 '23 at 15:31
  • 2
    might this be because `desc` is a reserved word in MySQL? https://www.tutorialspoint.com/why-can-t-we-use-column-name-desc-in-mysql#:~:text=The%20desc%20is%20a%20MySQL,let%20us%20create%20a%20table. – ACarter Jan 23 '23 at 15:32
  • likely that's the issue I think, see also https://stackoverflow.com/questions/8200174/cant-use-column-name-desc-in-mysql – ACarter Jan 23 '23 at 15:33

1 Answers1

1

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?

ACarter
  • 5,688
  • 9
  • 39
  • 56