Jul-24-2022, 02:44 PM
Hello, I need help, I had a goal to make a bot that would work with a database on SQLITE3, everything was connected, but in the process of interacting with the bot, an error occurs.All files that are used in the process are attached.
Error: return int(result[0][0])
IndexError: list index out of rangeCode file db.py:import sqlite3
class Database:
def __init__(self, db_file):
self.connection = sqlite3.connect(db_file)
self.cursor = self.connection.cursor()
def user_exists(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:
self.cursor.execute("INSERT INTO 'users' ('user_id') VALUES (?)", (user_id,))
def user_money(self, user_id):
with self.connection:
result = self.cursor.execute("SELECT 'money' FROM 'users' WHERE 'user_id' = ?", (user_id,)).fetchmany(1)
return int(result[0][0])code file main.pyimport logging
from aiogram import Bot, Dispatcher, executor, types
from aiogram.types.message import ContentTypes
from db import Database
import config as cfg
import markups as nav
logging.basicConfig(level=logging.INFO)
bot = Bot(token=cfg.TOKEN)
dp = Dispatcher(bot)
db = Database('database.db')
@dp.message_handler(commands=['start'])
async def start(message: types.Message):
if message.chat.type == 'private':
if not db.user_exists(message.from_user.id):
db.add_user(message.from_user.id)
await bot.send_message(message.from_user.id, f"Welcome user!\n Balance:{db.user_money(message.from_user.id)}$")
if __name__ == "__main__":
executor.start_polling(dp, skip_updates=True)
Attached Files
database.zip (Size: 403 bytes / Downloads: 319)
