1

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

3 Answers 3

2
connection.commit()

that will do the trick =D

answered Oct 8, 2011 at 6:11
Sign up to request clarification or add additional context in comments.

Comments

1

You need to commit the transaction.

answered Oct 8, 2011 at 6:06

Comments

0

Because you forgot to commit the transaction before closing the cursor.

answered Oct 8, 2011 at 6:05

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.