Melbourne2991
Melbourne2991

Reputation: 11797

Database Primary Key is incrementing, but no new rows are being added

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

Answers (3)

Marsh
Marsh

Reputation: 173

you use,

  db.commit()

after insert query

Upvotes: 1

Nisarg
Nisarg

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)

check here

Upvotes: 2

tusharmakkar08
tusharmakkar08

Reputation: 696

You can use

 cursor.autocommit(True)

in the beginning of the code for automatically committing the changes .

Upvotes: 1

Related Questions