[英]retrieving data from one table and putting into another PYTHON SQL
Trying to take a piece of data from at table in the database and inserting it into another table: 尝试从数据库中的at表中获取数据并将其插入另一个表中:
Fetching the order total and assigning it to variable: 获取订单总计并将其分配给变量:
cursor.execute('''SELECT Price FROM Tracks WHERE TrackID = ?''', (trackChoice,))
ordertotal = str(cursor.fetchall())
Putting it into table: 放入表格中:
cursor.execute('''INSERT INTO Orders(OrderID, Date, OrderTotal, CustomerID, TrackID) VALUES(?, ?, ?, ?, ?)''', (orderID, date,
ordertotal, customerID, trackChoice))
Error: 错误:
sqlite3.InterfaceError: Error binding parameter 2 - probably unsupported type.
With cursor.fetchall() you get all the rows in 'Tracks'. 使用cursor.fetchall(),您可以获取“轨道”中的所有行。 So it will have an ID and probably other informations as well. 因此,它将具有一个ID以及其他信息。 fE something like this: (1, 'William', 'Shakespeare', 'm', None, '1961-10-25'). 像这样:(1,“威廉”,“莎士比亚”,“ m”,“无”,“ 1961-10-25”)。 What is 'ordertotal'? 什么是“订单总额”? I guess it will be a number? 我猜会是数字吗? If yes, and you are using sqlite3 you could use row_factory. 如果是,并且您正在使用sqlite3,则可以使用row_factory。 See the answere here for more information: Get a list of field values from Python's sqlite3, not tuples representing rows 有关更多信息,请参见此处的答案: 从Python的sqlite3获取字段值的列表,而不是表示行的元组
Why not just do: 为什么不做:
cursor.execute("""
INSERT INTO Orders(name, Date, name, name, name)
VALUES(?, strftime('now'), ?, ?, ?)""",
(value, value, value, value);
That is, use the current time in the database. 也就是说,使用数据库中的当前时间。
(I assume that name
is really four different columns or you should get another error.) (我假设name
实际上是四个不同的列,否则您应该会遇到另一个错误。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.