class SQLighter:
def __init__(self, database):
self.connection = sqlite3.connect(database, check_same_thread = False)
self.cursor = self.connection.cursor()
self.cursor.execute("CREATE TABLE IF NOT EXISTS stats (user_id INT, user_name TEXT, survived INT, infected INT)")
def add_user(self, id, name, survived, infected):
with self.connection:
return self.cursor.execute("""INSERT INTO `stats` (user_id, user_name, survived, infected) VALUES (?,?,?,?)""", (id, name, survived, infected))
def get_nick(self, id):
with self.connection:
return self.cursor.execute("SELECT user_id FROM stats WHERE id = ?", (id, )).fetchall()
def set_fast(self, id):
with self.connection:
return self.cursor.execute("UPDATE stats SET infected=X WHERE id=?", (id))
sql_lighter.get_nick(754651730)
sql_lighter.get_nick(982543922)
def __init__(self, database):
self.connection = sqlite3.connect(database, check_same_thread = False)
self.cursor = self.connection.cursor()
self.cursor.execute("CREATE TABLE IF NOT EXISTS stats (user_id INT, user_name TEXT, survived INT, infected INT)")
self.connection.commit()
import sqlite3
from threading import Lock
lock = Lock()
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
class DataBase:
def __init__(self, _db):
self._db = _db
self.conn = sqlite3.connect(_db, check_same_thread=False)
self.conn.row_factory = dict_factory
self.cur = self.conn.cursor()
def add_row(self, table, **params):
item = []
p = []
values=[]
for key, value in params.items():
item.append(str(key))
values.append(str(value))
p.append("?")
item = ",".join(item)
p = ",".join(p)
with lock:
self.cur.execute(f"insert into {table} ({item}) values ({p})", values)
self.conn.commit()
def update_row(self, table, check=None, **params):
item, values = "", []
for key, value in params.items():
if item == "":
item = item + f"{key}=?"
else:
item = item + f",{key}=?"
values.append(value)
with lock:
if check is None:
self.cur.execute(f"update {table} SET {item}", values)
else:
self.cur.execute(f"update {table} SET {item} WHERE {check}", values)
self.conn.commit()
def get_row(self, table, row='*', check=None):
if check is None:
with lock:
respone = self.cur.execute(f"SELECT {row} FROM {table}")
return respone.fetchall()
else:
with lock:
respone = self.cur.execute(f"SELECT {row} FROM {table} WHERE {check}")
return respone.fetchall()
def get_lat_row(self, table):
with lock:
sql = f"SELECT * FROM {table} ORDER BY id DESC LIMIT 1"
rsp = self.cur.execute(sql)
return rsp.fetchone()
if __name__ == "__main__":
db = DataBase("base.db")
db.add_row("users", user_id=432, text="fdsfsd") # добавить запись
db.update_row("users", check="user_id=432", text="f")# обновляем запись с нужным узерид
db.get_row("users", check="user_id=432") #получаем запись с нужным юзерид
def add_user(self, id, name, survived, infected):
with self.connection:
obj = self.cursor.execute("""INSERT INTO `stats` (user_id, user_name, survived, infected) VALUES (?,?,?,?)""", (id, name, survived, infected))
self.commit()
return obj
conn = sqlite3.connect("db.db", check_same_thread=False)
cur = conn.cursor()
cur.execute("insert into users values ('name')"])
conn.commit
class SQLighter:
def __init__(self, database):
self.connection = sqlite3.connect(database, check_same_thread = False)
self.cursor = self.connection.cursor()
self.cursor.execute("CREATE TABLE IF NOT EXISTS stats (user_id INT, user_name TEXT, survived INT, infected INT)")
self.connection.commit()
def add_user(self, id, name, survived, infected):
with self.connection:
return self.cursor.execute("INSERT INTO `stats` (user_id, user_name, survived, infected) VALUES (?,?,?,?)", (id, name, survived, infected))
def get_nick(self, id):
with self.connection:
self.cursor.execute("SELECT user_id FROM stats WHERE user_id = ?", (id, )).fetchall()
self.connection.commit()
def set_fast(self, id, survived, infected):
with self.connection:
self.cursor.execute("UPDATE stats SET infected = ?, survived = ? WHERE user_id = ?", (infected, survived, id))
self.connection.commit()
sql_lighter = SQLighter("database.db")
sql_lighter.add_user(982543922, "Вова", 1, 1)
sql_lighter.add_user(754651730, "Андрей", 2, 2)
sql_lighter.set_fast(982543922, 1, 1)
sql_lighter.set_fast(754651730, 2, 2)
sql_lighter.get_nick(754651730)
sql_lighter.get_nick(982543922)
class SQLighter:
def __init__(self, database):
self.connection = sqlite3.connect(database, check_same_thread = False)
self.cursor = self.connection.cursor()
self.cursor.execute("CREATE TABLE IF NOT EXISTS stats (user_id INT, user_name TEXT, survived INT, infected INT)")
self.connection.commit()
def add_user(self, id, name, survived, infected):
with self.connection:
return self.cursor.execute("INSERT INTO `stats` (user_id, user_name, survived, infected) VALUES (?,?,?,?)", (id, name, survived, infected))
def get_nick(self, id):
with self.connection:
self.cursor.execute("SELECT user_id FROM stats WHERE user_id = ?", (id, )).fetchall()
self.connection.commit()
def set_fast(self, id, survived, infected):
with self.connection:
self.cursor.execute("UPDATE stats SET infected = ?, survived = ? WHERE user_id = ?", (infected, survived, id))
self.connection.commit()
sql_lighter = SQLighter("database.db")
sql_lighter.add_user(982543922, "Вова", 1, 1)
sql_lighter.add_user(754651730, "Андрей", 2, 2)
sql_lighter.set_fast(982543922, 1, 1)
sql_lighter.set_fast(754651730, 2, 2)
sql_lighter.get_nick(754651730)
sql_lighter.get_nick(982543922)
if message.text.lower() == ' мой профиль':
bot.send_message(message.chat.id, f"Мой профиль\nМой ID: [{message.from_user.id}]\nМой Псевдоним: [{message.from_user.username}]\nМоё Ф.И: [{message.from_user.first_name} {message.from_user.last_name}]\nКоличество побед за выжившего: [{survived}]\nКоличество заражений за заражённого: [{infected}]\nВерсия помощника бота: [V0.3]\nВерсия игры зомби: [V0.9]")
import sqlite3
class SQLighter:
def __init__(self, database):
self.connection = sqlite3.connect(database, check_same_thread=False)
self.cursor = self.connection.cursor()
self.cursor.execute(
"CREATE TABLE IF NOT EXISTS stats (user_id INT, user_name TEXT, survived INT, infected INT)")
self.connection.commit()
def add_user(self, id, name, survived, infected):
with self.connection:
obj = self.cursor.execute("INSERT INTO `stats` (user_id, user_name, survived, infected) VALUES (?,?,?,?)",
(id, name, survived, infected))
self.connection.commit()
return obj
def get_nick(self, id):
with self.connection:
self.cursor.execute("SELECT user_id FROM stats WHERE user_id = ?", (id,)).fetchall()
def set_fast(self, id, survived, infected):
with self.connection:
self.cursor.execute("UPDATE stats SET infected = ?, survived = ? WHERE user_id = ?",
(infected, survived, id))
self.connection.commit()
def delete_table(self, table):
with self.connection:
self.cursor.execute(f"DROP TABLE {table}")
self.connection.commit()
def create_table(self):
self.cursor.execute(
"CREATE TABLE IF NOT EXISTS stats (user_id INT, user_name TEXT, survived INT, infected INT)")
self.connection.commit()
sql_lighter = SQLighter("database.db")
sql_lighter.delete_table("stats")
sql_lighter.create_table()
sql_lighter.add_user(982543922, "Вова", 1, 1)
sql_lighter.add_user(754651730, "Андрей", 2, 2)
sql_lighter.set_fast(982543922, 1, 1)
sql_lighter.set_fast(754651730, 2, 2)
sql_lighter.get_nick(754651730)
sql_lighter.get_nick(982543922)
выжившего: [{survived}]\nКоличество заражений за заражённого: [{infected}]\nВерсия помощника бота: [V0.3]\nВерсия игры зомби: [V0.9]")
infected = 0
survived = 0
def get_infect_and_survived(self, id):
return self.cursor.execute(f'SELECT infected, survived FROM stats WHERE user_id={id}')
arr = sql_lighter.get_infect_and_survived(id) #здесь будет id
class SQLighter:
def __init__(self, database):
self.connection = sqlite3.connect(database, check_same_thread = False)
self.cursor = self.connection.cursor()
self.cursor.execute("CREATE TABLE IF NOT EXISTS stats (user_id INT, user_name TEXT, survived INT, infected INT)")
self.connection.commit()
def add_user(self, id, name, survived, infected):
with self.connection:
return self.cursor.execute("INSERT INTO `stats` (user_id, user_name, survived, infected) VALUES (?,?,?,?)", (id, name, survived, infected))
def get_nick(self, id):
with self.connection:
return self.cursor.execute("SELECT user_id FROM stats WHERE user_id = ?", (id, )).fetchall()
def set_fast(self, id, survived, infected):
with self.connection:
return self.cursor.execute("UPDATE stats SET infected = ?, survived = ? WHERE user_id=?", (infected, survived, id))
def get_infected_and_survived(self, id):
return self.cursor.execute(f'SELECT infected, survived FROM stats WHERE user_id={id}')
sql_lighter = SQLighter("database.db")
sql_lighter.add_user(982543922, "Вова", 1, 1)
sql_lighter.add_user(754651730, "Андрей", 2, 2)
sql_lighter.get_infected_and_survived(754651730)
sql_lighter.get_infected_and_survived(982543922)
infected = 0
survived = 0
sql_lighter.get_infected_and_survived(982543922)
def get_infected_and_survived(self, id):
for row in self.cursor.execute(f'SELECT infected, survived FROM stats WHERE user_id={id}'):
return row