简体   繁体   English

从一个表中检索数据并放入另一个PYTHON SQL

[英]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.

相关问题 使用python从一个sql表中使用名称来查找另一个sql表中的数据,然后将名称和数据组合成一个html表 - using python to use names from one sql table to find data in another and then combining the names and data to make an html table 使用python将mysql数据从一个表插入到另一个表 - Inserting mysql data from one table to another with python 将大量数据从一个表插入到另一个 MySQL Python - Insert bulk of data from one table to another MySQL Python 在连接的数据库中创建表并从另一个连接检索数据 - Creating a table in a database of a connection and retrieving data from another connection Python 将一个数组放入另一个数组时出现 ValueError - Python ValueError when putting one array into another 在 Flask 中将数据从一条路由连接到另一条路由,并使用 SQL-alchemy 将数据输入到 SQL 表中 - Connecting data from one route to another in Flask and inputting the data into the SQL table with SQL-alchemy 根据python变量的值从表中检索数据 - retrieving data from table based on a value from a python variable 将多个镶木地板文件中的数据检索到一个 dataframe (Python) - Retrieving data from multiple parquet files into one dataframe (Python) 从我的SQL检索数据并将其发送到使用python的Web服务 - Retrieving data from my sql and sending it to a web service using python Python和MSSQL:从SQL检索数据时的过滤技术 - Python and MSSQL: Filtering techniques while retrieving data from SQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM