Mysql 连接器 Python

Mysql Connector Python

Python , Mysql , Mysql连接器

import mysql.connector as sql

def Withdrawal():

     cn = sql.connect(host="localhost" ,
                      user="root",
                      passwd="toor",
                      database="Python_Bank")
    
     mycursor = cn.cursor()
     
     Withdraw = int(input(" Amount to be withdraw : "))
    
     Pin = input("Enter Your 4 Digit Pin : ")
    
     query = "Update login set Balance = Balance-'+Withdraw+' where Pin = '+Pin+' and Balance > '+Withdraw+'"
     
     mycursor.execute(query)
    
     print("\n Transaction Successfull ")

Error : 1292 (22007): Truncated incorrect DOUBLE value: '+Withdraw+'

描述登录;

你应该切换到准备好的语句,而不是在你的情况下错误的字符串连接。

 mycursor = cn.cursor(prepared=True,)
 Withdraw = int(input(" Amount to be withdraw : "))

 Pin = input("Enter Your 4 Digit Pin : ")
 query = """Update login SET Balance = Balance -%s WHERE Pin = '%s' AND Balance > %s;"""
 
 mycursor.execute(query,(Withdraw,Pin,Withdraw))