mardi 31 mars 2015

How to put lists together into list holding them as 'tuples'

In Pythons SQLite documents we have the following example on how to put many values into SQL database:



purchases = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
('2006-04-05', 'BUY', 'MSFT', 1000, 72.00),
('2006-04-06', 'SELL', 'IBM', 500, 53.00),
]
c.executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases)


But if I have one list containing the dates, one containing the buy/sell, one containing the stock tickers, one the amount and one the price, how to I combine them at the most optimal way, before I insert? I've tried looping over each list and just inserting them in the SQLite DB one by one, but that takes to much time.



dates = ['2006-03-28', '2006-04-05', '2006-04-06']
flags = ['buy', 'buy', 'sell']
tickers = ['IBM', 'MSFT', 'IBM']
amount = [1000, 1000, 500]
price = [45.00, 72.00, 53.00]


This takes too long:



for i in range(0, len(dates)):

c.executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', dates[i], flags[i], tickers[i], amount[i], price[i])

Aucun commentaire:

Enregistrer un commentaire