-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
54 lines (44 loc) · 1.36 KB
/
db.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
52
import sqlite3
class DB:
con = None
cursor = None
def __init__(self):
# или :memory: чтобы сохранить в RAM
self.con = sqlite3.connect("data.db")
self.cursor = self.con.cursor()
self.create()
def create(self):
tables = {
'users': (
'id integer PRIMARY KEY',
'email text',
'login text',
'password text',
'bill double'
),
'categories': (
'id integer PRIMARY KEY',
'name text',
'capacity integer',
'author integer'
),
'events': (
'id integer PRIMARY KEY',
'type text',
'amount double',
'category integer',
'date numeric',
'description text',
'author integer'
)
}
for name, columns in tables.items():
columns = ', '.join(map(str, columns))
self.cursor.execute(f'CREATE TABLE IF NOT EXISTS {name} ({columns})')
self.con.commit()
pass
def getCount(self, table, where):
sql = f'SELECT COUNT(*) FROM {table} WHERE {where}'
self.cursor.execute(sql)
result = self.cursor.fetchone()
return result[0]