'How do I solve this SQL query problem using Python?
When I run the following code I get the message "mysql.connector.errors.InterfaceError: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where AccNo=?' at line 1". The code is:
import mysql.connector
mydb=mysql.connector.connect(host='localhost', user='root', password='**********', database='bank_management', auth_plugin='mysql_native_password')
def OpenAcc():
n=input("Enter The Name: ")
ac=input("Enter The Account No: ")
db=input("Enter The Date Of Birth: ")
add=input("Enter The Address: ")
cn=input("Enter THe Contact Number: ")
ob=int(input("Enter The Opening Balance: "))
data1=(n,ac,db,add,cn,ob)
data2=(n,ac,ob)
sql1=('insert into account values (%s, %s, %s, %s, %s, %s)')
sql2= ('insert into amount values (%s, %s, %s)')
x=mydb.cursor()
x.execute(sql1,data1)
x.execute(sql2,data2)
mydb.commit()
print('Data Entered Successfully.')
main()
def DepoAmo():
amount=int(input("Enter the amount you want to deposit: "))
ac=input("Enter The Account No: ")
a='select Balance from amount where AccNo=%s'
data=(ac,)
x=mydb.cursor(prepared=True)
x.execute(a,data)
result=x.fetchone()
t=result[0] + amount
sql= 'update amount set Balance where AccNo=%s'
d=(t, ac)
x.execute(sql,d)
mydb.commit()
main()
def withdrawAmount():
amount = int(input('Enter the amount you want to withdraw: '))
ac = input("Enter The Account No: ")
a = 'select balance from amount where AccNo=%s'
data = (ac,)
x = mydb.cursor()
x.execute(a, data)
result = x.fetchone()
t = result[0] - amount
sql = ('update amount set Balance where AccNo=%s')
d = (t, ac)
x.execute(sql, d)
mydb.commit()
main()
def BalEnq():
ac=input("Enter the account no: ")
a='select * from amount where AccNo=%s'
data=(ac,)
x=mydb.cursor()
x.execute(a,data)
result=x.fetchone()
print("Balance for account:",ac,"is",result[-1])
main()
def DisDetails():
ac = input("Enter the account no: ")
a = 'select * from account where AccNo=%s'
data = (ac,)
x = mydb.cursor()
x.execute(a, data)
result = x.fetchone()
for i in result:
print(i)
main()
def CloseAcc():
ac = input("Enter the account no: ")
sql1='delete from account where AccNo=%s'
sql2='delete from amount where AccNo=%s'
data=(ac,)
x=mydb.cursor()
x.execute(sql1,data)
x.execute(sql2,data)
mydb.commit()
main()
def main():
print('''
1. OPEN NEW ACCOUNT
2. DEPOSIT AMOUNT
3. WITHDRAW AMOUNT
4. BALANCE ENQUIRY
5. DISPLAY CUSTOMER DETAILS
6. CLOSE AN ACCOUNT''')
choice =input("Enter The Task You Want To Perform: ")
if(choice=='1'):
OpenAcc()
elif(choice=='2'):
DepoAmo()
elif(choice=='3'):
withdrawAmount()
elif(choice=='4'):
BalEnq()
elif(choice=='5'):
DisDetails()
elif(choice=='6'):
CloseAcc()
else:
print("Invalid Choice")
main()
main()
The error message appears when I try to run the code for adding or withdrawing money. Can anyone see where I am going wrong? I think the error might be related to this line: a = 'select balance from amount where AccNo=%s'. There are two tables in the bank_management database: account and amount. The account table has 6 headings called Name, AccNo, DOB, Address, ContractNo, and OpeningBal. The amount table has three headings: Name, AccNo, Balance. If prepared=True is taken out of x=mydb.cursor() the following error appears: mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement
Solution 1:[1]
In your deposit method, the sql should be like: Set Balance=%s where accNo=%s
Same issue in withdraw amount method
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Colin Curtain |
