· 5 years ago · Aug 30, 2020, 06:38 AM
1# -* coding: utf-8 -*
2
3import sqlite3 as sql
4
5con = sql.connect("bot.db")
6
7
8
9
10
11
12
13
14default_db = """CREATE TABLE IF NOT EXISTS `user`(
15 `id` Integer primary key autoincrement,
16 `user_id` Integer NOT NULL unique,
17 `username` Varchar(30) NOT NULL unique,
18 `xp` Integer default 0,
19 `money` Integer default 0,
20 `role` Varchar(20) not null
21)"""
22
23class BaseBotDB:
24 def create_table(code_sql: str):
25 cur = con.cursor()
26 cur.execute(code_sql)
27 con.commit()
28
29 def add_user_to_db(user_id: int,
30 username: str, xp: int = 0, money: int = 0, role: str ="Турист"):
31 cur = con.cursor()
32 cur.execute("""INSERT INTO `user` (user_id, username, xp, money, role) VALUES(?, ?, ?, ?, ?)""", (user_id, username,xp, money, role))
33 con.commit()
34
35 def all_users():
36 cur = con.cursor()
37 return cur.execute("""SELECT * FROM `user`""").fetchall()
38 def delete_user(user_id: int = 0, username: str = ""):
39 if user_id:
40 types = f"user_id = {user_id}"
41 elif username:
42 types = f"username = {username}"
43 cur = con.cursor()
44 cur.exexute(f"DELETE FROM `user` WHERE {types}")
45 def get_user(user_id: int = 0, username: str = ""):
46 if user_id:
47 types = f"user_id = {user_id}"
48 elif username:
49 types = f"username = {username}"
50 cur = con.cursor()
51 return cur.execute(f"""SELECT * FROM `user` WHERE {types}""").fetchone()
52
53
54
55
56db = BaseBotDB
57
58
59
60
61
62db.create_table(default_db)
63db.add_user_to_db(570, "Roblus", role="Президент")
64
65print(db.all_users()
66)
67
68print(db.get_user(570))
69