0

I just got into SQL to do some data science and was wondering why my code was running but not affecting the MySQL database in any way. I am using pycharm and the MySQLdb module.

 import MySQLdb
 db = MySQLdb.connect(host="localhost",
 user="root",
 passwd="********", #Password blocked
 db="test")
 cur = db.cursor()
 cur.execute("SELECT * FROM movies")
 cur.execute("Update movies set genre = 'action' where id = 1")
 for row in cur.fetchall() :
 print row[0], " ", row[1], " ", row[2]

My code runs and returns no errors, but when I delete the

 cur.execute("Update movies set genre = 'action' where id = 1")

line it just prints out the table the as it was before. Just for reference, here is the table:

1 Interstellar sci-fi

2 Thor: Ragnarok action

3 Thor: The Dark World action

How can I make the commands in python actually affect the table? Thank you so much for your help!

asked Nov 11, 2017 at 1:20

2 Answers 2

1

You have to commit changes made to the table using cur.commit()

Database does not update automatically with MySQL and Python

answered Nov 11, 2017 at 1:56
Sign up to request clarification or add additional context in comments.

Comments

0

Use cur.commit() to commit the changes to the database. Also, you should close your database using cur.close() once you're finished.

answered Nov 11, 2017 at 2:40

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.