forked from Dishni95/jingle_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
106 lines (94 loc) · 3.1 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
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
import sqlite3
# dictionary_db functions
def delete_one_message(chat):
my_conn = sqlite3.connect('dictionary.db')
csr = my_conn.cursor()
csr.execute("DELETE FROM messages WHERE chay_id = (?)", (chat,))
my_conn.commit()
my_conn.close()
def add_message(chat, message):
my_conn = sqlite3.connect('dictionary.db')
csr = my_conn.cursor()
csr.execute("INSERT INTO messages VALUES (?,?)", (chat, message))
my_conn.commit()
my_conn.close()
def add_user(user):
my_conn = sqlite3.connect('dictionary.db')
csr = my_conn.cursor()
csr.execute("INSERT OR IGNORE INTO users VALUES (?)", (user,))
my_conn.commit()
my_conn.close()
def add_one(word):
my_conn = sqlite3.connect('dictionary.db')
csr = my_conn.cursor()
word_0 = word[0]
csr.execute("INSERT INTO dictionary VALUES (?,?,?,?,?)", word_0)
my_conn.commit()
my_conn.close()
def show_all(table):
my_conn = sqlite3.connect('dictionary.db')
csr = my_conn.cursor()
csr.execute(f"SELECT rowid, * FROM {table}")
items = csr.fetchall()
for i in items:
print(i)
my_conn.commit()
my_conn.close()
def list_of_words(user):
l = []
my_conn = sqlite3.connect('dictionary.db')
csr = my_conn.cursor()
csr.execute("SELECT rowid, * FROM dictionary WHERE user_id = (?)", (user,))
items = csr.fetchall()
for i in items:
(a,b,c) = (i[1],i[4],i[3])
l.append((a,b,c))
my_conn.commit()
my_conn.close()
return l
def lookup(new_word, user):
my_conn = sqlite3.connect('dictionary.db')
csr = my_conn.cursor()
csr.execute("SELECT * from dictionary WHERE word = (?) AND user_id = (?)", (new_word, user))
items = csr.fetchall()
l = len(items)
print(l)
if l == 0:
my_conn.commit()
my_conn.close()
return False
else:
my_conn.commit()
my_conn.close()
return True
def add_count(new_word, user):
my_conn = sqlite3.connect('dictionary.db')
csr = my_conn.cursor()
csr.execute(f"SELECT count FROM dictionary WHERE word = (?) AND user_id = (?)", (new_word, user))
count = csr.fetchone()[0]
print(count)
count_add = count + 1
csr.execute(f"UPDATE dictionary SET count = (?) WHERE word = (?) AND user_id = (?)", (count_add, new_word, user))
my_conn.commit()
my_conn.close()
def update_weight(new_word, user):
my_conn = sqlite3.connect('dictionary.db')
csr = my_conn.cursor()
csr.execute("SELECT weight FROM dictionary WHERE word = (?) AND user_id = (?)", (new_word, user))
weight = csr.fetchone()[0]
print(weight)
weight_pop = weight - 1
csr.execute("UPDATE dictionary SET weight = (?) WHERE word = (?) AND user_id = (?)", (weight_pop, new_word, user))
my_conn.commit()
my_conn.close()
def list_of_messages(chat_id):
list = []
my_conn = sqlite3.connect('dictionary.db')
csr = my_conn.cursor()
csr.execute("SELECT message_id FROM messages WHERE chay_id = (?)", (chat_id,))
messages = csr.fetchall()
for i in messages:
list.append(i[0])
my_conn.commit()
my_conn.close()
return list