Feb-06-2018, 02:26 AM
My code checks if an account number exists and if so just prints a statement saying so, else if not then it asks for a name and then stores the new account number and name.
I have defined the data type for the fields both as TEXT. My program works if I only enter a single digit but if I enter a number of 2 or more digits then I get an error. But what is confusing to me is that I can enter a name for customer name with many characters and it stores fine even though both fields are defined as TEXT. I also tried defining the account number field as INTEGER and REAL and I get the same result.
Here's my code:
I have defined the data type for the fields both as TEXT. My program works if I only enter a single digit but if I enter a number of 2 or more digits then I get an error. But what is confusing to me is that I can enter a name for customer name with many characters and it stores fine even though both fields are defined as TEXT. I also tried defining the account number field as INTEGER and REAL and I get the same result.
Here's my code:
import sqlite3 conn = sqlite3.connect('example.db') c = conn.cursor() def create_table(): c.execute('CREATE TABLE IF NOT EXISTS customers_main(acctnum TEXT, custname TEXT)') def manual_input(): account = input("Enter account number: ") c.execute("SELECT acctnum from customers_main WHERE acctnum=?", (account)) result = c.fetchone() if result: print('Customer in database!') else: customername = input('Enter customer name: ') c.execute("INSERT INTO customers_main VALUES (?, ?)", (account, customername)) conn.commit() create_table() manual_input() c.close() conn.close()And here's the error:
Error:=================== RESTART: /home/pi/Documents/forum1.py ===================
Enter account number: 1
Enter customer name: Don
>>>
=================== RESTART: /home/pi/Documents/forum1.py ===================
Enter account number: 1
Customer in database!
>>>
=================== RESTART: /home/pi/Documents/forum1.py ===================
Enter account number: 1234
Traceback (most recent call last):
File "/home/pi/Documents/forum1.py", line 24, in <module>
manual_input()
File "/home/pi/Documents/forum1.py", line 12, in manual_input
c.execute("SELECT acctnum from customers_main WHERE acctnum=?", (account))
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 4 supplied.
What am I doing wrong?