简体   繁体   中英

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:

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'. So it will have an ID and probably other informations as well. fE something like this: (1, 'William', 'Shakespeare', 'm', None, '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. See the answere here for more information: Get a list of field values from Python's sqlite3, not tuples representing rows

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM