-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot_database.py
63 lines (53 loc) · 2.06 KB
/
bot_database.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
# -*- coding: utf-8 -*-
import sqlite3
DBFILE = 'data.db'
def init_db():
conn = sqlite3.connect(DBFILE)
c = conn.cursor()
c.execute('CREATE TABLE IF NOT EXISTS users (user_id integer primary key, username text, request_step integer, request_string text);')
c.execute('CREATE TABLE IF NOT EXISTS bot_answers (user_id integer primary key, answer text, count integer, date integer);')
#c.execute('ALTER TABLE users ADD username text;')
conn.commit()
conn.close()
def get_alldata():
conn = sqlite3.connect(DBFILE)
c = conn.cursor()
c.execute('SELECT * FROM users;')
result = c.fetchall()
conn.close()
return result
def get_userdata(user_id):
conn = sqlite3.connect(DBFILE)
c = conn.cursor()
c.execute('SELECT * FROM users WHERE user_id=?;', (user_id,))
result = c.fetchone()
conn.close()
return result
def set_userdata(user_id, userdata):
conn = sqlite3.connect(DBFILE)
c = conn.cursor()
c.execute("INSERT OR IGNORE INTO users (user_id, username, request_step, request_string) VALUES(?, ?, ?, ?);", (user_id, userdata.username, userdata.request_step, userdata.request_string))
c.execute("UPDATE users SET request_step = ?, username = ?, request_string = ? WHERE user_id = ?;", (userdata.request_step, userdata.username, userdata.request_string, user_id))
conn.commit()
conn.close()
def get_answer(user_id):
conn = sqlite3.connect(DBFILE)
c = conn.cursor()
c.execute('SELECT * FROM bot_answers WHERE user_id=?;', (user_id,))
result = c.fetchone()
conn.close()
return result
def set_answer(user_id, answer):
conn = sqlite3.connect(DBFILE)
c = conn.cursor()
c.execute("INSERT OR IGNORE INTO bot_answers (user_id, answer, count, date) VALUES(?, ?, ?, ?);", (user_id, answer.answer.decode('utf-8'), answer.count, answer.date))
c.execute("UPDATE bot_answers SET answer = ?, count = ?, date = ? WHERE user_id = ?;", (answer.answer.decode('utf-8'), answer.count, answer.date, user_id))
conn.commit()
conn.close()
def get_answers():
conn = sqlite3.connect(DBFILE)
c = conn.cursor()
c.execute('SELECT * FROM bot_answers;')
result = c.fetchall()
conn.close()
return result