shivam mitra
shivam mitra

Reputation: 232

Not able to insert into mysql database

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?

Upvotes: 0

Views: 56

Answers (1)

Lawrence Benson
Lawrence Benson

Reputation: 1406

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

Upvotes: 3

Related Questions