mysql.connector.errors.ProgrammingError: 1064 (4200): You have an error in your SQL syntax;

Viewed 44912

I am attempting to insert data into a MySQL database. I am using python 2.7 and I am using the mysql.connector.

My error is:

mysql.connector.errors.ProgrammingError: 1064 (4200): 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 '%s) at line 1.

This indicates a error in my code where I attempt to insert my variable np (VALUES (%s)");). np is a "noun-phrase" such as a "skate board".

import mysql.connector
from textblob import TextBlob

cnx = mysql.connector.connect(user='XXX', password='XXX',
                              host='XXXXX',
                              database='XXXX')

cursor = cnx.cursor(buffered=True)

Latest = ("SELECT * FROM SentAnalysis")
cursor.execute(Latest)

for row in cursor.fetchall():
    SentText = row[2]
    blob = TextBlob(SentText)
    for np in blob.noun_phrases:
        print(np)
        SQLInsertCmd = ("INSERT INTO TestNounPhrase (NPhrase) VALUES (%s)")
        cursor.execute(SQLInsertCmd,np)

cnx.commit()
cursor.close()
cnx.close()

The example from the manual is https://dev.mysql.com/doc/connector-python/en/connector-python-example-cursor-transaction.html. e.g.

 "VALUES (%s, %s, %s, %s, %s)")

I can't see a difference. This error is also discussed in detail here : How can I fix MySQL error #1064? Other similar examples on stackoverflow have been linked to reserved words, Redundant comas .But looking at these examples I can't spot an obvious error.

Any suggestions on where I am going wrong would be much appreciated.

10 Answers

I think the issue here might be the semicolon at the end of the query.

Have a good day.

str parameter in SQL query must by in quote 'str'.
So, need use '%s' with ' ' for str, and %s without ' ' for numbers:

cursor.execute("""INSERT INTO db_name (str,int,str,int)
                  VALUES ('%s', %s, '%s', %s)""" % (str,int,str,int))
cnx.commit()

You have to convert the tuple which contains the value to insert into a 'full tuple'. e.g.:

for np in blob.noun_phrases:
        np=(np,)

In a generic example, if you just have one column to insert, it would be:

to_insert=('A value to insert',)

Lastly, if you had multiple values to insert at a time:

to_insert_multiple=[('value1',), ('value2',), ('valueN',)]

I hope it helps, it worked for me in py3.7

I had the same issue with the single parameter statement, this worked:

mycursor.execute("INSERT INTO cust (user) VALUES(%s)" % (username))

I was getting same error and while searching for its solution I stumbled upon this question after several attempts I was able to resolve. The mistake was a typing error for column name in WHERE clause of UPDATE statement. My column name was 'ROLL_NO' instead I had typed 'ROLL_NO.' Please look to this point as well.

when calling the execute() method:

  cursor.execute(query, (param))

should be:

  cursor.execute(query, (param,))

I had a quite a struggle with this error because one of the column-name was exit. Be aware that database will accept this as a column name but when you try to insert from python, you need to cover the column in with backticks `

you should give %s value like this:

"INSERT INTO LaptopPrice (Name) VALUES ('%s')" % value

if you have few values you have to use tuple of your valuse like this code: "INSERT INTO LaptopPrice (Name, Price, Size, Weight, ScreenSize, Resolution, CPU, RAM, RAMType, Memory, MemoryType, GraphicCard) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')" % tuple(values)

mycursor.execute("insert into table_name(column) values(%s)"%data_variable)

Related