-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataBassSQL.py
79 lines (65 loc) · 2.17 KB
/
dataBassSQL.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import psycopg2 as sql
thatCursor = None
con = None
def exeAndGet(*arg):
for e in arg:
thatCursor.execute(e)
return thatCursor.fetchall()
def isTableExist(nameTable:str) -> bool:
return exeAndGet(f"SELECT EXISTS (SELECT FROM pg_tables WHERE schemaname = 'public' AND tablename = '{nameTable}');")[0][0]
def exeAndCommit(*arg):
for e in arg:
thatCursor.execute(e)
con.commit()
def initData(dbName: str, dbUser: str, dbPassword: str, dbHost: str, dbPort : str):
global thatCursor, con
con = sql.connect(database=dbName,
user=dbUser,
password=dbPassword,
host=dbHost,
port=dbPort)
thatCursor = con.cursor()
if not isTableExist("gng"):
print("SQL : gng table not found...",end='')
exeAndCommit("CREATE TABLE gng ( \
iduser BIGINT, \
remain INT DEFAULT 7, \
iscorrect BOOLEAN DEFAULT FALSE, \
istroll BOOLEAN, \
correct BIGINT \
); \
")
print("completed")
if not isTableExist("verify"):
print("SQL : verify table not found...",end='')
exeAndCommit("CREATE TABLE verify ( \
iduser BIGINT, \
num SMALLINT, \
code TEXT \
); \
")
print("completed")
if not isTableExist("music"):
print("SQL : music table not found...",end='')
exeAndCommit("CREATE TABLE music ( \
serverid BIGINT, \
vcid BIGINT, \
link TEXT, \
title TEXT, \
path TEXT, \
len REAL,\
sid BIGINT\
); \
")
print("completed")
if __name__ == "__main__":
envConfig = dict()
with open(".env","r") as f:
for line in f:
if line.strip()[0] != "#" and '=' in line:
envConfig[line.split('=')[0]] = ("=".join(line.split('=')[1:])).strip()
initData(envConfig["DB_DATABASE"], envConfig["DB_USERNAME"],
envConfig["DB_PASSWORD"], envConfig["DB_HOST"],
envConfig["DB_PORT"])
res = checkTableExist("gng")
print(res, type(res))