Can't add values to mysql database

Advertisements

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)

>Solution :

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

Leave a ReplyCancel reply