0

I have just started using MySQLdb in python. I am able to create table but when I try to insert, no rows are inserted into the table and its shows that table is still empty.

    import MySQLdb
    db = MySQLdb.connect("localhost","root","shivam","test")
    cursor = db.cursor()
    s = "DROP TABLE IF EXISTS batting"
    cursor.execute(s)
    s = """create table batting (
        name varchar(50) primary key,
        matches integer(5),
        innings integer(5),
        runs integer(5),
        highest integer(3),
        strikerate integer(3),
        hundreds integer(3),
        fifties integer(3)
        )"""
    cursor.execute(s)
    s = """insert into batting(name) values(
        "shivam")"""

cursor.execute(s)
db.close()

Where I could be going wrong?

shivam mitra
  • 232
  • 1
  • 5
  • 12

1 Answers1

3

You forgot to commit your connection. Simply add:

cursor.execute(s)
db.commit()

Have a look at this. It explains why you need to commit

Community
  • 1
  • 1
Lawrence Benson
  • 1,398
  • 1
  • 16
  • 33