Home > Database > Mysql Tutorial > Why Does My Python Script Fail to Update a MySQL Database Even Though It Shows Rows Updated?

Why Does My Python Script Fail to Update a MySQL Database Even Though It Shows Rows Updated?

DDD
Release: 2024-11-03 08:07:30
Original
740 people have browsed it

Why Does My Python Script Fail to Update a MySQL Database Even Though It Shows Rows Updated?

Database Update Issue in Python Script Resolved

In a recent query, it was discovered that a Python script designed to update a MySQL database was erroneously not updating the table. Upon further investigation, the script was found to contain the following code:

<code class="python">dbb = MySQLdb.connect(host="localhost", 
       user="user", 
       passwd="pass", 
       db="database") 
try:
   curb = dbb.cursor()
   curb.execute ("UPDATE RadioGroups SET CurrentState=1 WHERE RadioID=11")
   print "Row(s) were updated :" +  str(curb.rowcount)
   curb.close()
except MySQLdb.Error, e:
   print "query failed<br/>"
   print e  </code>
Copy after login

The script encountered issues despite declaring that the rows were updated. The cause of this discrepancy was found to be the absence of dbb.commit(). To ensure that updates performed using a Python script are correctly applied to the MySQL database, it is crucial to incorporate dbb.commit() after using curb.execute. This function commits all the changes that you 'loaded' into the MySQL server, making them permanent in the database.

By implementing this change, the Python script will successfully update the MySQL table as intended.

The above is the detailed content of Why Does My Python Script Fail to Update a MySQL Database Even Though It Shows Rows Updated?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template