如何检查用户输入是否在SQL表中?



我要求客户的ID来检查他们是否在表中,但是我1)不知道我是否正确,2)不断得到一个值错误

def place_order():
custid_Sorder = int(input("Please enter your customer ID: "))
custid_query = "SELECT * FROM Customer WHERE CustomerID=?"
cursor.execute(custid_query, custid_Sorder)
result_po = cursor.fetchall()
connection.commit()
temp = False
for x in result_po:
if custid_Sorder in x:
temp = True
if temp:
print("Welcome to our Christmas store! This is all of our stock;")
cursor.execute("SELECT * FROM Inventory")
stock = cursor.fetchall()
for row in stock:
print(row)
buy_another_flag = 1
while buy_another_flag != 0:
option = int(input("Enter the ID of the item you would like to order: "))
option_quant = int(input("Enter quantity: "))

输入客户id,就会出现

cursor.execute(custid_query, custid_Sorder)
ValueError: parameters are of unsupported type

试试这个

custid_query = "SELECT * FROM Customer WHERE CustomerID=?"
cursor.execute(custid_query, [custid_Sorder])

如果你正在使用psycopg (postgres),你也可以这样做

custid_query = "SELECT * FROM Customer WHERE CustomerID = %s"
cursor.execute(custid_query, (custid_Sorder,))

最新更新