Hi,
I want to know how to use Python variables in My sql statement and below is the syntax i am using
import MySQLdb
name="XYZ"
number="123456"
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()

but this throws the error
please refer the attached screen shot for the error and help me out

Recommended Answers

All 13 Replies

Thanx for the information.
Now i get different error
if i user INSERT statement as
sql= ("""INSERT INTO phonebook(number, Mobile) VALUES(%s,%s)""" , name,number)
cursor.execute(sql)
i get the below error
exceptions:TyprError:execute statemenmt takes atmost 3 arguments (4 given)
and if i modify the insert statement as
sql= ("""INSERT INTO phonebook(number, Mobile) VALUES(%s,%s)""" , %(name,number))

i get another exception as
exceptions:TyprError:execute statemenmt takes atmost 3 arguments (61 given)

Please help me out
thanks in advance

Thanx for the information.
Now i get different error
if i user INSERT statement as
sql= ("""INSERT INTO phonebook(number, Mobile) VALUES(%s,%s)""" , name,number)
cursor.execute(sql)
i get the below error
exceptions:TyprError:execute statemenmt takes atmost 3 arguments (4 given)
and if i modify the insert statement as
sql= ("""INSERT INTO phonebook(number, Mobile) VALUES(%s,%s)""" , %(name,number))

i get another exception as
exceptions:TyprError:execute statemenmt takes atmost 3 arguments (61 given)

Please help me out
thanks in advance

Did you try with *sql as I wrote above ?

Yes i have tried... below is my code

import MySQLdb
name="XYZ"
number="123456"
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()

please let me if there is another way to use python variables in MySQL insert statement

which works!!

thnx for the info please let me know what changes needs to be done for my code to work

thnx for the info please let me know what changes needs to be done for my code to work

Probably

sql= ("""INSERT INTO phonebook(number, Mobile) VALUES("%s","%s")""", (name, number))
cursor.execute(*sql)

If there is only one value, use sql = ("...", (name,))

for only one variable it works but how to handle multiple variables.
i googled this and tried many ways nothing is working for multiple variables.
please help me out

for only one variable it works but how to handle multiple variables.
i googled this and tried many ways nothing is working for multiple variables.
please help me out

The example I wrote above has 2 variables. Doesn't it work ?

no it doesn't work

no it doesn't work

OOps, reading further the link I gave, you , it seems that there are no quotes around the %s, try

sql= ("""INSERT INTO phonebook(number, Mobile) VALUES (%s,%s)""", (name, number))
cursor.execute(*sql)

what does python say ?

it's working thanx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.