-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql.py
86 lines (76 loc) · 3.47 KB
/
sql.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
80
81
82
83
84
85
86
import sqlite3
class DatabaseQuery:
def __init__(self, db_name):
self.db_name = db_name
def create_table(self):
conn = sqlite3.connect(self.db_name)
cursor = conn.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS users ( userid VARCHAR(8) NOT NULL, status VARCHAR(8) NOT NULL DEFAULT "clean", user_to_chat VARCHAR(8), chat_status VARCHAR(20) NOT NULL DEFAULT "not active", photo BOOLEAN NOT NULL DEFAULT "TRUE", age INT(3) DEFAULT NULL, gender VARCHAR(1) DEFAULT NULL);')
conn.commit()
conn.close()
def register_user(self, chat_id):
conn = sqlite3.connect(self.db_name)
cursor = conn.cursor()
try:
cursor.execute("SELECT * FROM users WHERE userid = '{}'".format(chat_id)).fetchone()[0]
except:
cursor.execute("INSERT INTO users (userid) VALUES ('{}')".format(chat_id))
conn.commit()
conn.close()
def active_user(self, chat_id):
conn = sqlite3.connect(self.db_name)
cursor = conn.cursor()
cursor.execute("UPDATE users SET chat_status = 'active' WHERE userid = '{}'".format(chat_id))
conn.commit()
conn.close()
def check_connect(self, chat_id):
conn = sqlite3.connect(self.db_name)
cursor = conn.cursor()
user_to_chat = cursor.execute("SELECT userid FROM users WHERE chat_status = 'active' AND NOT userid = '{}'".format(chat_id)).fetchone()
if user_to_chat:
cursor.execute("UPDATE users SET chat_status = 'occuped', user_to_chat = '{}' WHERE userid = '{}'".format(user_to_chat[0], chat_id))
cursor.execute("UPDATE users SET chat_status = 'occuped', user_to_chat = '{}' WHERE userid = '{}'".format(chat_id, user_to_chat[0]))
conn.commit()
conn.close()
return True
else:
conn.close()
return False
def end_conversation(self, chat_id):
conn = sqlite3.connect(self.db_name)
cursor = conn.cursor()
cursor.execute("UPDATE users SET chat_status = 'not active', user_to_chat = 'NULL' WHERE userid = '{}'".format(chat_id))
cursor.execute("UPDATE users SET chat_status = 'not active', user_to_chat = 'NULL' WHERE user_to_chat = '{}'".format(chat_id))
conn.commit()
conn.close()
def set_pics(self, chat_id, opt):
conn = sqlite3.connect(self.db_name)
cursor = conn.cursor()
cursor.execute("UPDATE users SET photo = '{}' WHERE userid = '{}'".format(opt, chat_id))
conn.commit()
conn.close()
def check_pics(self, chat_id):
conn = sqlite3.connect(self.db_name)
cursor = conn.cursor()
check = cursor.execute("SELECT photo FROM users WHERE userid = '{}'".format(chat_id)).fetchone()[0]
conn.close()
return check
def get_user_to_chat(self, chat_id):
conn = sqlite3.connect(self.db_name)
cursor = conn.cursor()
try:
check = cursor.execute("SELECT user_to_chat FROM users WHERE userid = '{}'".format(chat_id)).fetchone()[0]
conn.close()
if check:
return check
else:
return 0
except:
conn.close()
return 0
def get_chat_status(self, chat_id):
conn = sqlite3.connect(self.db_name)
cursor = conn.cursor()
status = cursor.execute("SELECT chat_status FROM users WHERE userid = '{}'".format(chat_id)).fetchone()[0]
conn.close()
return status