0

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

2
Contributors
13
Replies
15
Views
6 Years
Discussion Span
Last Post by hisan
0

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

0

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 ?

0

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()

0

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

which works!!

0

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

0

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,))

Edited by Gribouillis: n/a

0

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

0

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 ?

0

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 ?

Edited by Gribouillis: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.