Hi I have this basic mysql code:
sql = 'INSERT INTO popularity (PersonNumber, Category, Value) VALUES (1,"Tennis","great")'
connection = MySQLdb.connect(host = "localhost", user = "***",
passwd = "***", db = "inb104")
cursor = connection.cursor()
cursor.execute(sql)
cursor.close()
connection.close()
However, this doesn't insert anything to the database. why? The sql syntax is correct, because I have coppied the outputted sql variable into the mysql console directly. I know this code is working, because If I do: sql =' SELECT * FROM popularity' I can output the result in python.
asked Oct 8, 2011 at 6:01
dgamma3
2,3694 gold badges27 silver badges49 bronze badges
3 Answers 3
connection.commit()
that will do the trick =D
Sign up to request clarification or add additional context in comments.
Comments
You need to commit the transaction.
answered Oct 8, 2011 at 6:06
varunl
20.4k5 gold badges33 silver badges47 bronze badges
Comments
Because you forgot to commit the transaction before closing the cursor.
answered Oct 8, 2011 at 6:05
Ignacio Vazquez-Abrams
804k160 gold badges1.4k silver badges1.4k bronze badges
Comments
default