-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstocks_query
More file actions
37 lines (22 loc) · 783 Bytes
/
stocks_query
File metadata and controls
37 lines (22 loc) · 783 Bytes
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
'''
'''
import sqlite3
conn = sqlite3.connect('stocks.db')
cursor = conn.cursor()
# To retrieve the data, you have two approaches
# Approach 1
symbol = 'RHAT'
cursor.execute("SELECT * FROM stocks where symbol_text = '%s'" % symbol)
print(cursor.fetchone())
# Approach 2 - This is the preferred approach
val = ('RHAT',)
cursor.execute('SELECT * FROM STOCKS where symbol_text=?', val)
print(cursor.fetchone())
# If you need to insert many records at a time, then follow this approach
purchases = [('2018-03-28', 'BUY', 'IBM', 1000, 45.00),
('2018-04-05', 'BUY', 'MSFT', 1000, 72.00),
('2018-03-16', 'SELL', 'IBM', 500, 53.00)
]
cursor.executemany('INSERT INTO stocks VALUES (?, ?, ?, ?, ?)', purchases)
conn.commit()
conn.close()