Reputation: 11797
I am writing my first python script, and I am trying to connect it to a mysql db to insert rows.
import MySQLdb
db = MySQLdb.connect("localhost","root","xxx","pytest" )
cursor = db.cursor()
cursor.execute("INSERT INTO `first_table` (`name`) VALUES ('boop') ")
When I check the mysql db via phpmyadmin, it contains no rows, however if the auto incrementing ID was 5 and then I run the script 2 times, when I insert a new row it inserts it as id= 8 so the script has been incrementing the primary key but not inserting the rows?
The script reports no mysql errors, so I'm a bit lost here.
Upvotes: 1
Views: 337
Reputation: 3252
In yuor case please use
import MySQLdb
db = MySQLdb.connect("localhost","root","jimmypq79","pytest" )
cursor = db.cursor()
cursor.execute("INSERT INTO `first_table` (`name`) VALUES ('boop') ")
db.commit()
Please put this in top of the code like this--
db = MySQLdb.connect("localhost","root","jimmypq79","pytest" )
db.autocommit(True)
Upvotes: 2
Reputation: 696
You can use
cursor.autocommit(True)
in the beginning of the code for automatically committing the changes .
Upvotes: 1