Mysql Connector Python

Viewed 175

Python , Mysql , MysqlConnector

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+'

desc login;

enter image description here

1 Answers

You should switch to prepared statements instead of in your case wrong stirng concatenation.

 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))
Related