Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to insert some data into a MySQL DB with Python and MySQLdb. When i do the following:

query = "INSERT INTO universitats (universitat) VALUES ('%s')" % (lloc)
cursor.execute(query)
db.commit()

I get this error:

Traceback (most recent call last):
  File "read.py", line 39, in <module>
    cursor.execute(query)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
_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 syntax to use near 'Hospitalet de Llobregat')' at line 1")

What am I doing wrong?

share|improve this question
    
can you also provide some of the lines of code preceding query=... which can help explain the value of lloc and the design of universitats –  samkhan13 Jul 18 '13 at 22:20

1 Answer 1

up vote 1 down vote accepted

This lines:

 query = "INSERT INTO universitats (universitat) VALUES ('%s')" % (lloc)
 cursor.execute(query)

should look like this

query = "INSERT INTO universitats (universitat) VALUES (%s)"  
cursor.execute(query,(lloc,))

and then commit.

share|improve this answer
    
Thanks! It worked! –  Paolo Jul 19 '13 at 6:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.