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.
code file main.py
Error: return int(result[0][0])
IndexError: list index out of range
Code file db.py:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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 ]) |
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 |
import 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
