-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
63 lines (43 loc) · 1.2 KB
/
database.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
53
54
55
56
57
58
59
60
61
62
63
import sqlite3
import os
DB_PATH = os.environ.get('DB_PATH', 'database.sqlite')
def __run_write_query(query):
connection = __get_connection()
cursor = connection.cursor()
cursor.execute(query)
connection.commit()
connection.close()
def __get_connection():
db_path = DB_PATH
connection = sqlite3.connect(db_path)
return connection
def database_initialize():
create_users = """
CREATE TABLE IF NOT EXISTS users (
telegram_id INTEGER PRIMARY KEY,
lichess_username TEXT
)
"""
__run_write_query(create_users)
def database_update(query):
connection = __get_connection()
cursor = connection.cursor()
cursor.execute(query)
connection.commit()
connection.close()
def database_fetchone(query):
connection = __get_connection()
cursor = connection.cursor()
cursor.execute(query)
result = cursor.fetchone()
connection.commit()
connection.close()
return result
def database_fetchall(query):
connection = __get_connection()
cursor = connection.cursor()
cursor.execute(query)
result = cursor.fetchall()
connection.commit()
connection.close()
return result