Shiva
Shiva

Reputation: 43

How to use python variables in MySQL Insert statement, i am getting a tuple error, Please suggest

import MySQLdb
name="XYZ"
number=(256, 34576312114897154715004917944343995880721156274004613128261928143013598386679L)
db=MySQLdb.Connect("localhost","root","12345","phone")
cursor=db.cursor()
sql= ("""INSERT INTO phonebook(number, Mobile) VALUES(%s,%s)""" , name,number)
cursor.execute(sql)
db.commit()
db.close()

"Failed processing format-parameters; %s" % err) mysql.connector.errors.ProgrammingError: Failed processing format-parameters; Python 'tuple' cannot be converted to a MySQL type

Upvotes: 4

Views: 11242

Answers (1)

Neel
Neel

Reputation: 21253

Your sql variable is a tuple

sql= ("""INSERT INTO phonebook(number, Mobile) VALUES(%s,%s)""" , name,number)

So when it try to execute, cursor.execute(sql) (where sql is tuple) it give error Python 'tuple' cannot be converted to a MySQL type

Please use string.

sql = """INSERT INTO phonebook(number, Mobile) VALUES(%s,%s)""" % (name,number)

Upvotes: 2

Related Questions