IndexError: list index out of range - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: IndexError: list index out of range (/thread-37802.html) |
IndexError: list index out of range - dolac - Jul-24-2022 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 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.py 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) RE: IndexError: list index out of range - woooee - Jul-24-2022 IndexError: list index out of range 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])First test result for length to see if anything was retrieved. Second, print result to see if/what result[0][0] would be. RE: IndexError: list index out of range - deanhystad - Jul-25-2022 Why are you using quotes inside your queries? I expect a query to be formatted like this: result = self.cursor.execute("SELECT money FROM users WHERE user_id = ?", (user_id,)).fetchmany(1)I guess you can make a table where all the columns have starting and ending quotes, but why? And why are you using fetchmany(1) instead fetchone()? return self.cursor.execute("SELECT money FROM users WHERE user_id = ?", (user_id,)).fetchone() RE: IndexError: list index out of range - dolac - Jul-25-2022 (Jul-25-2022, 05:00 AM)deanhystad Wrote: Why are you using quotes inside your queries? Thanks, it was all about the quotes that were in the db.py file. RE: IndexError: list index out of range - deanhystad - Jul-25-2022 Quote:Thanks, it was all about the quotes that were in the db.py file.Huh? That is not and answer to "Why are you using quotes inside your queries?" Do you mean that the quotes in the queries were the reason why you got an index error? Even if your code works now, indexing should always be checked or protected to prevent an empty reply from crashing your program. |