crk_telegram_bot/app/database/db_connector.py

65 lines
2.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import sqlite3
class ALLusers:
def __init__(self, database):
"""Подключаемся к БД и сохраняем курсор соединения"""
self.connection = sqlite3.connect(database)
self.cursor = self.connection.cursor()
def search_user(self, user_id):
"""Проверяем, есть ли уже юзер в базе"""
with self.connection:
result = self.cursor.execute('SELECT * FROM `users` WHERE `user_id` = ?', (user_id,)).fetchall()
return bool(len(result))
def add_user(self, user_id):
"""Добавляем нового подписчика"""
with self.connection:
return self.cursor.execute("INSERT INTO `users` (`user_id`) VALUES(?)", (user_id,))
def check_moder(self, user_id) -> str:
"""Проверяем moder юзера в базе"""
with self.connection:
result = self.cursor.execute('SELECT `moder` FROM `users` WHERE `user_id` = ?', (user_id,)).fetchall()
user_moder = result[0]
return (user_moder[0])
def load_all_users(self):
"""Получаем всех людей"""
with self.connection:
return self.cursor.execute("SELECT * FROM `users`").fetchall()
def log_info(self):
"""Подсчет пользователей"""
with self.connection:
result = self.cursor.execute("SELECT COUNT(DISTINCT id) FROM `users`").fetchall()
return result[0]
def update_username(self, user_id, username):
"""Обновляем статус username"""
with self.connection:
return self.cursor.execute(f"UPDATE users SET name = ? WHERE user_id = ?", (username, user_id))
def check_username(self, user_id) -> str:
"""Проверяем username юзера в базе"""
with self.connection:
result = self.cursor.execute('SELECT `name` FROM `users` WHERE `user_id` = ?', (user_id,)).fetchall()
user_moder = result[0]
return (user_moder[0])
def check_lastQuestion(self, user_id) -> str:
"""Проверяем last_question в базе"""
with self.connection:
result = self.cursor.execute('SELECT `last_question` FROM `users` WHERE `user_id` = ?', (user_id,)).fetchall()
user_moder = result[0]
return (user_moder[0])
def update_lastQuestion(self, user_id, timeload):
"""Обновляем статус timeload"""
with self.connection:
return self.cursor.execute(f"UPDATE users SET last_question = ? WHERE user_id = ?", (timeload, user_id))
def close(self):
"""Закрываем соединение с БД"""
self.connection.close()