I am using python MySQL API to connect to Mysql database from python program. I am facing a problem from few days. I am unable to insert records into the database and dont know whats the reason. Here is the way i connect and insert records into the database.
db = MySQLdb.connect("localhost","root","padmaramulu","pdfsearch" ) cursor = db.cursor() #cursor.execute("""CREATE TABLE IF NOT EXISTS documents (docid INT NOT NULL ,PRIMARY KEY(docid),docname CHAR(30)) engine=innodb""") temp = "hello";number = 2; cursor.execute( 'insert into documents(docid,docname) values("%d","%s")' % (number,temp) ) db.close()
Why is it so?
executemany() Method. This method prepares a database operation (query or command) and executes it against all parameter sequences or mappings found in the sequence seq_of_params . In Python, a tuple containing a single value must include a comma.
connect ( 'mydatabase. db' ) cursor = conn. cursor () #create the salesman table cursor. execute("CREATE TABLE salesman(salesman_id n(5), name char(30), city char(35), commission decimal(7,2));") s_id = input('Salesman ID:') s_name = input('Name:') s_city = input('City:') s_commision = input('Commission:') cursor.
Before closing the connection, you should add db.commit()
.
if you are using InnoDB engine in MySQL you should add
db.commit()
else change your MySQL engine into MyISAM no need to change anything in code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With