-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdb_helper.py
51 lines (44 loc) · 1.35 KB
/
db_helper.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def get_database_file(arguments):
if arguments.database:
database_file = arguments.database
else:
fd, database_file = tempfile.mkstemp()
os.close(fd)
return database_file
def initialize(database):
cursor = database.cursor()
cursor.execute(
"""
DROP TABLE IF EXISTS balance
"""
)
cursor.execute(
"""
CREATE TABLE balance (
address TEXT PRIMARY KEY,
amount BIGINT NOT NULL,
height BIGINT NOT NULL
)
"""
)
return cursor
def add_address(address, amount, height, cursor):
cursor.execute("""
INSERT OR IGNORE INTO balance (amount, height, address) VALUES (?, ?, ?)
""", (0, 0, address))
cursor.execute("""
UPDATE balance SET
amount = amount + ?,
height = ?
WHERE address = ?
""", (amount, height, address))
def fetch_addresses_from_database(cursor, arguments):
if arguments.sort is None:
select_expression = 'SELECT * FROM balance'
elif arguments.sort == 'ASC':
select_expression = 'SELECT * FROM balance ORDER BY amount ASC'
elif arguments.sort == 'DESC':
select_expression = 'SELECT * FROM balance ORDER BY amount DESC'
else:
select_expression = 'SELECT * FROM balance'
cursor.execute(select_expression)