1

Hi I'm trying to insert some data into a MySQL database. However I keep getting a syntax error from the MySQL.connector. I'm not sure what is causing this I've tried all the examples on the mysql website but none of them work. I could get this to work on my old web server for a different website but now its not working.

My code is (the ID is a 30 string of upper-case letters and numbers and the UserID is a 20 digit string):

sql = '''INSERT INTO LoginSessions(SessionID,UserID) VALUES({0},{1})'''.format(ID,UserID)
cursor.execute(sql)

This is the error I keep getting:

<class '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 ''ORPFDQE0CKYVY1LN0RPW',))' at line 1 

Thanks for any help in advance. It will be greatly appreciated it's been bugging me for a few hours. I've tried everything I possibly know and am about to give up!

1
  • 1
    "I've tried everything I possibly know". Please add "print and examine the resulting SQL string" to your toolbelt. Commented May 25, 2014 at 17:29

1 Answer 1

1

You shouldn't use format to create a sql-command, use placeholders, then you don't have to take care, that the types of your IDs is correctly encoded:

sql = '''INSERT INTO LoginSessions(SessionID,UserID) VALUES(%s,%s)'''
cursor.execute(sql, (ID,UserID))
Sign up to request clarification or add additional context in comments.

4 Comments

ive tried that and i get this error <class 'mysql.connector.errors.ProgrammingError'>: Failed processing format-parameters; 'MySQLConverter' object has no attribute '_tuple_to_mysql'
What are the types of ID and UserID, really?
they should be strings but im not sure what they are any more do you know how to find that out
Hi figured it out for some reason the UserID was a tuple

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.