-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbworker.py
187 lines (151 loc) · 5.08 KB
/
dbworker.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import sqlite3
import config
def addUser(user_id, t_name, t_username):
conn = sqlite3.connect(config.db_file)
cursor = conn.cursor()
cursor.execute("select tid from USERS where tid=" + str(user_id))
result = cursor.fetchall()
if not result:
cursor.execute("insert into USERS (tid, name, username, state, remind_time) values ('" + str(user_id) + "', '" +
t_name + "', '" + t_username + "', 0,15)")
cursor.execute("update USERS set state=0 where tid=" + str(user_id))
conn.commit()
conn.close()
def getState(user_id):
conn = sqlite3.connect(config.db_file)
cursor = conn.cursor()
cursor.execute("select state from USERS where tid=" + str(user_id))
result = cursor.fetchone()
if not result:
return "0"
conn.commit()
conn.close()
return result
def setLastNode(user_id):
conn = sqlite3.connect(config.db_file)
cursor = conn.cursor()
cursor.execute("select count(*) from reminders where owner_id=" + str(user_id))
num = cursor.fetchone()[0]
conn.commit()
conn.close()
conn = sqlite3.connect(config.db_file)
cursor = conn.cursor()
cursor.execute("update USERS set last_note_id='" + str(num) + "' where tid=" + str(user_id))
conn.commit()
conn.close()
def getLastNoteId(user_id):
conn = sqlite3.connect(config.db_file)
cursor = conn.cursor()
cursor.execute("select id from reminders where owner_id=" + str(user_id) + " order by id desc ")
result = cursor.fetchone()[0]
if not result:
return "0"
conn.commit()
conn.close()
return result
def getLastNotesId(user_id):
conn = sqlite3.connect(config.db_file)
cursor = conn.cursor()
cursor.execute("select id from reminders where owner_id=" + str(user_id) + " order by id desc")
result = cursor.fetchall()
if not result:
return "-"
conn.commit()
conn.close()
return result[0][0]
def getPrevNoteId(note_id):
conn = sqlite3.connect(config.db_file)
cursor = conn.cursor()
cursor.execute("select prev_remind from reminders where id=" + str(note_id))
result = cursor.fetchall()
if not result:
return "0"
conn.commit()
conn.close()
return result
def getNextNoteId(note_id):
conn = sqlite3.connect(config.db_file)
cursor = conn.cursor()
cursor.execute("select prev_remind from reminders where id=" + str(note_id))
result = cursor.fetchall()
if not result:
return "0"
conn.commit()
conn.close()
return result
def getRemindDate(note_id):
conn = sqlite3.connect(config.db_file)
cursor = conn.cursor()
cursor.execute("select remind_date from reminders where id=" + str(note_id))
result = cursor.fetchall()
if not result:
return '0'
conn.commit()
conn.close()
return result[0]
def getReminds(date, time):
conn = sqlite3.connect(config.db_file)
cursor = conn.cursor()
cursor.execute("select owner_id, text from reminders where remind_date='" + str(date) + "' and remind_time='"
+ str(time) + "'")
result = cursor.fetchall()
if not result:
return None
conn.commit()
conn.close()
return result
def getRemindTime(note_id):
conn = sqlite3.connect(config.db_file)
cursor = conn.cursor()
cursor.execute("select remind_time from reminders where id=" + str(note_id))
result = cursor.fetchall()
if not result:
return "0"
conn.commit()
conn.close()
return result[0]
def deleteNote(note_id):
conn = sqlite3.connect(config.db_file)
cursor = conn.cursor()
cursor.execute("delete from reminders where id=" + str(note_id))
conn.commit()
conn.close()
def getNotesById(note_id):
conn = sqlite3.connect(config.db_file)
cursor = conn.cursor()
cursor.execute("select text from reminders where id=" + str(note_id))
result = cursor.fetchall()
if not result:
return "0"
conn.commit()
conn.close()
return result
def addNote(user_id, text):
conn = sqlite3.connect(config.db_file)
cursor = conn.cursor()
prev_id = getLastNotesId(user_id)
cursor.execute("insert into reminders (owner_id, text, remind_date, remind_time, prev_remind) values ("
+ str(user_id) + ", '" + text + "', '0', '0', '" + str(prev_id) + "')")
conn.commit()
conn.close()
setLastNode(user_id)
def setDate(user_id, value):
conn = sqlite3.connect(config.db_file)
cursor = conn.cursor()
lni = getLastNotesId(user_id)
cursor.execute("update reminders set remind_date='" + str(value) + "' where id=" + str(lni))
conn.commit()
conn.close()
def setTime(user_id, value):
conn = sqlite3.connect(config.db_file)
cursor = conn.cursor()
lni = getLastNoteId(user_id)
cursor.execute("update reminders set remind_time='" + str(value) + "' where id=" + str(lni))
conn.commit()
conn.close()
def setState(user_id, value):
conn = sqlite3.connect(config.db_file)
cursor = conn.cursor()
cursor.execute("update USERS set state='" + str(value) + "' where tid=" + str(user_id))
conn.commit()
conn.close()