【问题标题】:retrieving data from one table and putting into another PYTHON SQL从一个表中检索数据并放入另一个 PYTHON SQL
【发布时间】:2017-10-07 15:12:44
【问题描述】:

试图从数据库中的表中取出一条数据并将其插入到另一个表中:

获取订单总额并将其分配给变量:

cursor.execute('''SELECT Price FROM Tracks WHERE TrackID = ?''', (trackChoice,))
ordertotal = str(cursor.fetchall())

放入表中:

cursor.execute('''INSERT INTO Orders(OrderID, Date, OrderTotal, CustomerID, TrackID) VALUES(?, ?, ?, ?, ?)''', (orderID, date, 
ordertotal, customerID, trackChoice))

错误:

sqlite3.InterfaceError: Error binding parameter 2 - probably unsupported type.

【问题讨论】:

标签: python sql sqlite sql-insert


【解决方案1】:

使用 cursor.fetchall() 可以获取“Tracks”中的所有行。所以它会有一个ID,可能还有其他信息。 f.E.像这样的东西:(1,'威廉','莎士比亚','m',无,'1961-10-25')。什么是“订单总数”?我想这将是一个数字?如果是,并且您使用的是 sqlite3,则可以使用 row_factory。更多信息请看这里的回答者:Get a list of field values from Python's sqlite3, not tuples representing rows

【讨论】:

    【解决方案2】:

    为什么不直接做:

    cursor.execute("""
    INSERT INTO Orders(name, Date, name, name, name)
        VALUES(?, strftime('now'), ?, ?, ?)""",
                       (value, value, value, value);
    

    即使用数据库中的当前时间。

    (我假设name 确实是四个不同的列,否则您应该得到另一个错误。)

    【讨论】:

    • 我确实写了这个错误:NameError: name 'strftime' is not defined。我加了“时间”。在前面得到: sqlite3.InterfaceError: Error binding parameter 2 - 可能是不支持的类型。
    • 是的,我只是使用名称来确保所讨论的列和值之间没有混淆
    • @AntsOfTheSky 。 . .忽略那个。函数调用应该在VALUES() 内。我只是放错地方了。
    猜你喜欢
    • 2021-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-04
    • 1970-01-01
    • 2016-03-07
    相关资源
    最近更新 更多