Reputation: 25
Here's the code:
a1 = input('Index : ')
b1 = input('Name : ')
c1 = input('job : ')
sql3 = 'Insert into data(index,name,job) values(%s, %s, %s)'
val3 = [a1,b1,c1]
cursor.execute(sql3,val3)
mydb.commit()
for x in cursor:
print(x)
I have entered the values as follows:
a=3
b=sam
c=singer
And i'm getting this error:
ProgrammingError: 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 'index,name,phone_no) values('3', 'sam', 'singer')' at line 1
Please help me with this..
P.S: Please don't judge me by this question, I am trying to learn these codes myself...
Upvotes: 1
Views: 92
Reputation: 16081
For executemany
val3
should be a list of list, like this
sql3 = 'INSERT INTO data(index,name,phone_no) VALUES (%s, "%s", "%s")'
val3 = [[a1,b1,c1]] # val shoul be a list of list
cursor.executemany(sql3,val3)
If you have one set of values I would suggest to use cursor.execute
,
sql3 = f'INSERT INTO data (index,name,phone_no) VALUES ({a1}, "{b1}", "{c1}")'
cursor.execute(sql3)
Without f-string
,
sql3 = 'INSERT INTO data(index,name,phone_no) VALUES (%s, "%s", "%s")'
val3 = (a1,b1,c1) # val shoul be a list of list
cursor.executemany(sql3, val3)
Upvotes: 2