6
votes

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.

7
If you only have a single entry for VALUES I believe you need a comma in your tuple: VALUES (%s,). - andrew_reece
I updated to include a comma and I still get the same error. - Steve
I speculate that the issue is at least partly due to that you are trying to select and insert at the same time. Question upvoted +1. - Tim Biegeleisen
Is there a way to avoid a select in this scenario? The select currently extracts the information required to run the "Noun Phrase" analysis . The output of this analysis is then inserted into another table. I have used select and inserts in the same script before without issue e.g. selecting data-> running sentiment analysis on the data-> then inserting the sentiment results into a new table. - Steve
The correct way to fix it is to convert the tuple which contains the value to insert into a 'full tuple'. e.g.: - Hernán Borré

7 Answers

6
votes

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

Have a good day.

6
votes

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()
2
votes

Try this

SQLInsertCmd = """INSERT INTO
                  TestNounPhrase (NPhrase) VALUES ((%s))"""  % (np)
cursor.execute(SQLInsertCmd)
2
votes

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

0
votes

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

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

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.

-2
votes

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