1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| import sqlite3 conn = sqlite3.connect('example.db') c = conn.cursor()
c.execute('''CREATE TABLE stocks (date text, trans text, symbol text, qty real, price real)''')
c.execute("INSERT INTO stocks VALUES ('2016-06-27', 'BUY', 'APPL', 100, 100)")
conn.commit()
conn.close()
conn = sqlite3.connect('example.db') c = conn.cursor()
t = ('APPL',) c.execute('SELECT * FROM stocks WHERE symbol=?', t) print c.fetchone()
purchases = [('2016-06-01', 'BUY', 'IBM', 100, 45.00), ('2016-06-01', 'BUY', 'GOOG', 100, 450.00), ] c.executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases)
for row in c.execute('SELECT * FROM stocks ORDER BY price'): print row
|