-1
我正在尝试使用MySQLdb将数据输入到MySQL表中,但它并未将数据插入表中。 我的代码是:Python - MySQLdb - 未插入表中的数据
try:
db = MySQLdb.Connect("127.0.0.1","root","root","bank")
cursor = db.cursor()
cursor.execute("SELECT max(account_number) from pybank")
results = cursor.fetchall()
for row in results:
self.account_number = row[0]
sql = "INSERT INTO pybank(account_number,user_name, user_age, user_dob, amount) VALUES(%d,'%s',%d,'%s',%d)" %(self.account_number + 1,self.user_name, self.user_age, self.user_dob, self.total_amount)
print(sql)
cr = db.cursor()
cr.execute(sql)
db.commit()
print("Your have successfully created your account")
self.getAccountDetails()
except:
db.rollback()
print("Your account is not created !!!! ")
print
print("Please try again")
删除您try-except块,并再次尝试得到一个回溯。 – wenzul
@wenzul感谢您的帮助。它解决了我的问题。 –
为了安全起见,请查看http://stackoverflow.com/questions/1947750/does-python-support-mysql-prepared-statements。 – wenzul