pythondatabaseaiogramaiosqlite

Can not add any data into aiosqlite database table


I am creating a bot using aiogram library. I need to store users. I decided not to use sqlite3 but aiosqlite, because it is asynchronous. Here's the code for the handler:

@all_router.message(Command('start'))
async def cmd_start(message: Message):
    load_dotenv()
    kb = None
    if message.from_user.id == int(os.getenv('ADMIN_ID')):
       kb = admin_keyboard
    else: 
       kb = reply_menu_keyboard
       users = Users()
       await users.add_user(message.from_user.id)
    await message.answer(await start_text(message.from_user.username), parse_mode='html', reply_markup=kb)

And the code for the db method:

    async def start(self) -> None:
        async with aiosql.connect(self.path) as con:
            request = '''CREATE TABLE IF NOT EXISTS users (
                          user_telegram_id INTEGER PRIMARY KEY NOT NULL,
                          status TEXT NOT NULL)'''
            await con.execute(request)

    async def add_user(self, uid: int) -> None:
        '''The method adds a user into the table'''
        await self.start()
        async with aiosql.connect(self.path) as con:
            cursor = await con.cursor()
            request = 'INSERT INTO users(user_telegram_id, status) VALUES(?, ?)'
            await cursor.execute(request, (uid, 'member'))

However, whenever I run /start nothing happens! It does not add anything into the table and shows no error. I don't know what may be wrong.

I've been using sqlite3 for a while now, but never have I faced such an issue. I really need your help, the mistake might well be very silly.


Solution

  • You should commit every query like this

    con.commit()
    

    It lets a user permanently save all the changes made in the transaction of a database or table.