sqlite 我试图向表中插入值,但唯一约束失败,如何解决此问题?

o8x7eapl  于 2022-12-19  发布在  SQLite
关注(0)|答案(1)|浏览(139)

[上一个错误]

line 138, in place_order
    cursor.execute("INSERT INTO 'Order' (OrderNo, CustomerID, Date, TotalCost) VALUES (?,?,?,?)", (
sqlite3.InterfaceError: Error binding parameter 3 - probably unsupported type

[当前错误]

cursor.execute("INSERT INTO 'Order' (OrderNo, CustomerID, Date, TotalCost) VALUES (?,?,?,?)", (
sqlite3.IntegrityError: UNIQUE constraint failed: Order.OrderNo, Order.CustomerID

我得到这个错误,但我不完全确定它的意思,在这个错误发生之前,我得到一个错误,说我试图插入的数据类型是'不支持'。我认为这可能是与SQL解释订单作为一个内置函数?这是代码的错误弹出。(我用SQLITE3顺便说一句)

buy_another_flag = 2
ordernum = 0
while buy_another_flag != 1:
    option = int(input("Enter the ID of the item you would like to order: "))
    option_quant = int(input("Enter quantity: "))
    now = str(datetime)
    ordernum += 1
    sql_cost = "SELECT UnitPrice FROM Inventory WHERE ItemID=?"
    cursor.execute(sql_cost, [option])
    connection.commit()
    price_for_item = cursor.fetchall()
    total_item_price = str(price_for_item * option_quant)
    cursor.execute("INSERT INTO 'Order' (OrderNo, CustomerID, Date, TotalCost) VALUES (?,?,?,?)", (
        ordernum, custid_Sorder, now, total_item_price))
    connection.commit()
buy_another_flag = int(input("Would you like to purchase another item?\n1. No\n2. Yes\n> "))

这是我想要插入值的表

tblorder = """ CREATE TABLE IF NOT EXISTS "Order"
        (
         OrderNo INT,
         CustomerID INT,
         Date TEXT,
         TotalCost FLOAT,
         primary key(OrderNo, CustomerID)
         foreign key(CustomerID) references Customer(CustomerID)
         )"""

cursor.execute(tblorder)
connection.commit()

谢谢你。

cbeh67ev

cbeh67ev1#

如果你真的把你的表命名为ORDER,那么你将不得不永远地逃避它:

sql = 'INSERT INTO "Order" (OrderNo, CustomerID, Date, TotalCost) VALUES (?,?,?,?)'
cursor.execute(sql, (ordernum, custid_Sorder, now, total_item_price,))

ORDER是SQLite中的保留关键字,您应该避免使用它来命名表。

相关问题