jemjem
jemjem

Reputation: 13

Can't add values to mysql database

I tried entering values to mysql database using python. In python there is no error but when I check in mysql the items haven't been added yet. This is the code

import mysql.connector as mc mc.connect(host='localhost',user='root',password='password',database='school') c=mc.cursor() cursor.execute('insert into class values({},'{}',{})).format(rollno,name,fees)

Upvotes: 1

Views: 16

Answers (1)

Retro_Zapper
Retro_Zapper

Reputation: 53

Your code shouldn't have worked properly beacuse you did not name your connection object. Anyways this issue is because you did not commit your transaction. Try this:

import mysql.connector as mc 
mycon=mc.connect(host='localhost',user='root',password='password',database='school') 
c=mc.cursor() cursor.execute('insert into class values({},'{}',{})').format(rollno,name,fees)
mycon.commit()

Whenever you insert or delete records from mysql always commit the transaction or it won't reflect changes

Upvotes: 1

Related Questions