Home >Database >Mysql Tutorial >How to Correctly Insert Integer Data into a MySQL Database Using Python?
You want to insert data into your MySQL database using integers. However, the code you provided is encountering an issue.
The code:
import MySQLdb conn = MySQLdb.connect(host= "localhost", user="root", passwd="newpassword", db="engy1") x = conn.cursor() x.execute("SELECT * FROM anooog1") x.execute (" INSERT INTO anooog1 VALUES ('%s','%s') ", (188,90)) row = x.fetchall()
fails to insert data into your database because of the following error:
To resolve this issue, the code should be modified as follows:
import MySQLdb # Connect to the database conn = MySQLdb.connect(host="localhost", user="root", passwd="newpassword", db="engy1") x = conn.cursor() # Insert data into the database try: x.execute("INSERT INTO anooog1 VALUES (%s,%s)", (188, 90)) conn.commit() except: conn.rollback() # Close the connection conn.close()
The updated code includes the following changes:
The above is the detailed content of How to Correctly Insert Integer Data into a MySQL Database Using Python?. For more information, please follow other related articles on the PHP Chinese website!