-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_local.py
153 lines (112 loc) · 3.73 KB
/
database_local.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import sqlite3
import os
db = sqlite3.connect(f'chat_application_local.db')
cursor = db.cursor()
def drop_db():
db.close()
os.remove("chat_application_local.db")
def create_table():
sql = f'''
CREATE TABLE contacts (
ID INTEGER PRIMARY KEY AUTOINCREMENT,
user_name VARCHAR(256) NOT NULL,
email VARCHAR(256) NOT NULL,
status VARCHAR(256),
profile_picture VARCHAR(256)
);'''
cursor.execute(sql)
db.commit()
sql = ''' CREATE TABLE chats (
ID INTEGER PRIMARY KEY AUTOINCREMENT,
user_name VARCHAR(256) NOT NULL,
message VARCHAR(256) NOT NULL,
date VARCHAR(256) NOT NULL,
time VARCHAR(256) NOT NULL,
received BOOLEAN NOT NULL
);
'''
cursor.execute(sql)
db.commit()
sql = ''' CREATE TABLE settings (
ID INTEGER PRIMARY KEY AUTOINCREMENT,
key VARCHAR(256) NOT NULL,
value VARCHAR(256) NOT NULL
);
'''
cursor.execute(sql)
db.commit()
def create_and_drop_table():
drop_db()
global db, cursor
db = sqlite3.connect('chat_application_local.db')
cursor = db.cursor()
create_table()
def get_all_contacts():
sql = "SELECT * FROM contacts"
cursor.execute(sql)
db.commit()
contacts = [contact for contact in cursor]
return contacts
def get_contact(username):
sql = f"SELECT * FROM contacts WHERE user_name=='{username}'"
cursor.execute(sql)
db.commit()
return cursor.fetchall()
def get_user(detail):
sql = f"SELECT * FROM contacts WHERE user_name=='{detail}' OR email=='{detail}'"
cursor.execute(sql)
db.commit()
return cursor.fetchall()
def get_chats(username):
sql = f"SELECT * FROM chats WHERE user_name=='{username}'"
cursor.execute(sql)
db.commit()
return cursor.fetchall()
def uploadprofilepicture(username, profilepicture):
sql = f"UPDATE contacts SET profile_picture='{profilepicture}' WHERE user_name='{username}'"
cursor.execute(sql)
# try:
# cursor.execute(sql)
# return True
# except:
# return False
# finally:
# db.commit()
def add_message(message, contact, now):
date = now.date()
time = str(now.time()).split('.')[0]
sql = f"INSERT INTO chats(user_name, message, date, time, received) VALUES('{contact}', '{message}', '{date}', '{time}', 'False')"
cursor.execute(sql)
db.commit()
return True
def search_user(search_query):
sql = f"SELECT * FROM contacts WHERE user_name LIKE '{search_query}%'"
cursor.execute(sql)
db.commit()
return cursor.fetchall()
def add_user(user):
sql = f"INSERT INTO contacts(user_name, email, status, profile_picture) VALUES('{user[1]}', '{user[2]}', '{user[3]}', '{user[4]}')"
cursor.execute(sql)
db.commit()
return True
def get_setting(key):
sql = f"SELECT * FROM settings WHERE key='{key}'"
try:
cursor.execute(sql)
return cursor.fetchone()[2]
except:
return False
finally:
db.commit()
def set_setting(key, value):
sql = f"SELECT * FROM settings WHERE key='{key}'"
cursor.execute(sql)
if cursor.fetchone():
sql = f"UPDATE settings SET value='{value}' WHERE key='{key}'"
cursor.execute(sql)
db.commit()
else:
sql = f"INSERT INTO settings(key, value) VALUES('{key}', '{value}')"
cursor.execute(sql)
db.commit()
return True