I have searched high and low on this site and many others and have found similar questions, but none of the answers have worked for me (usually just accounting for the tuple). I'm writing a python script to parse html pages and populate a database. I have almost everything working except the populating part...
Here is the code segment that deals with the mySQL database (note: using MySQLdb module in python)
conn = MySQLdb.connect(user="root", passwd="xxxxx",db="nutrients")
cur = conn.cursor()
test = "Canned Corn"
cur.execute("INSERT INTO food (name) VALUES (%s)", (test,))
conn.commit()
I was first testing it with parsed string but that wasn't working. This gives me 2 errors:
Traceback (most recent call last): File "C:\Python32\lib\site-packages\MySQLdb\cursors.py", line 171, in execute r = self._query(query) File "C:\Python32\lib\site-packages\MySQLdb\cursors.py", line 330, in _query rowcount = self._do_query(q) File "C:\Python32\lib\site-packages\MySQLdb\cursors.py", line 294, in _do_query db.query(q) _mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax ; check the manual that corresponds to your MySQL server version for the right s yntax to use near '%s)' at line 1")
Traceback (most recent call last): File "C:\Python32\lib\site-packages\MySQLdb\cursors.py", line 171, in execute r = self._query(query) File "C:\Python32\lib\site-packages\MySQLdb\cursors.py", line 330, in _query rowcount = self._do_query(q) File "C:\Python32\lib\site-packages\MySQLdb\cursors.py", line 294, in _do_query db.query(q) _mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax ; check the manual that corresponds to your MySQL server version for the right s yntax to use near '%s)' at line 1")
Why does this query not work!?
Update: After nearly pulling my hair out I decided to revert back to 2.7.3 and guess what... everything works now :D Should have know it was an error of that type... Thanks for the help none the less everyone!
%sisn't really substituted with the value oftest. - Lev Levitsky(test,)as the second argument toexecuteor if you have another%sin the query. - Lev Levitsky