Reputation: 53
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!
Upvotes: 0
Views: 90
Reputation: 1
Use cur.commit()
to commit the changes to the database. Also, you should close your database using cur.close()
once you're finished.
Upvotes: 0
Reputation: 339
You have to commit changes made to the table using cur.commit()
Database does not update automatically with MySQL and Python
Upvotes: 1