-
Notifications
You must be signed in to change notification settings - Fork 0
/
module_db_words.py
70 lines (59 loc) · 2.03 KB
/
module_db_words.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
from config_db import *
def check_if_word_exists(id, word):
try:
with connection.cursor() as cursor:
query = "SELECT * FROM db_word WHERE id=%s AND word=%s"
val = (id, word)
cursor.execute(query, val)
result = cursor.fetchall()
return result
except:
raise Exception("db_words Error")
def update_quantity(id, word, quantity=0):
try:
with connection.cursor() as cursor:
query = "UPDATE db_word SET quantity=%s WHERE id=%s AND word=%s"
val = (quantity, id, word)
cursor.execute(query, val)
connection.commit()
except:
raise Exception("db_words Error")
def insert_word(id, word):
try:
with connection.cursor() as cursor:
query = "INSERT INTO db_word (id, word, quantity) VALUES (%s, %s, %s)"
val = (id, word, '1')
cursor.execute(query,val)
connection.commit()
except:
raise Exception("db_words Error")
def get_words_sorted_descending_order(id):
try:
with connection.cursor() as cursor:
query = "SELECT word FROM db_word WHERE id=%s ORDER BY quantity desc"
val = (id)
cursor.execute(query, val)
result = cursor.fetchall()
return result
except:
raise Exception("db_words Error")
def get_words_quantity_sorted_descending_order(id):
try:
with connection.cursor() as cursor:
query = "SELECT word, quantity FROM db_word WHERE id=%s ORDER BY quantity desc"
val = (id)
cursor.execute(query, val)
result = cursor.fetchall()
return result
except:
raise Exception("db_words Error")
def word_update_or_insert(id, word):
try:
answer = check_if_word_exists(id, word)
if answer != ():
quantity = answer[0]["quantity"] + 1
update_quantity(id, word, quantity)
else:
insert_word(id, word)
except Exception as e:
return e