Hey All,
Using an example from the Python DOCs:
stocks = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
('2006-04-05', 'BUY', 'MSOFT', 1000, 72.00),
('2006-04-06', 'SELL', 'IBM', 500, 53.00),
]:
for t in stocks
c.execute('insert into stocks values (?,?,?,?,?)', t)
In my code, the stocks from above is generated from a query to another DB.
Since tuples are immutable, how do you pass additional values to the cursor execute statement (in addition to the tuple).
Is there a better solution then the example below?:
stocks = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
('2006-04-05', 'BUY', 'MSOFT', 1000, 72.00),
('2006-04-06', 'SELL', 'IBM', 500, 53.00),
]:
for t in stocks
t = list(t)
t.append('Some Arb Value')
t = tuple(t)
c.execute('insert into stocks values (?,?,?,?,?,?)', t)
You could also do this:
stocks = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
('2006-04-05', 'BUY', 'MSOFT', 1000, 72.00),
('2006-04-06', 'SELL', 'IBM', 500, 53.00),
]:
for t in stocks
c.execute('insert into stocks values (?,?,?,?,?,?)', (t[0],t[1],t[2],t[3],t[4],'some value')
However, the solutions above wont work for the executemany method i.e
c.executemany('insert into stocks values (?,?,?,?,?,?)', t)
Is there a better way of doing this?