pythonexecuteaiogramasyncpg

conn does not have an execute method


import aiogram
from aiogram import Router
import asyncpg
import asyncio
from data.config import Config

Data_base = Router()


@Data_base.message()
async def connenct():
    conn = asyncpg.connect(r"postgresql://postgres@localhost/user")

    return conn


@Data_base.message()
async def including_data_from_user(connect, message):
    conn = await connect()
    await conn.execute(f'''INSERT INTO user (first_name,
    last_name,
    age,
     telegram_id
     ) 
     VALUES ({message.from_user.first_name},
    {message.from_user.last_name},
    1,
    {message.from_user.id})''')

In my code, I tried to call a database connection when calling one method, but I cannot make changes to it since conn does not have an execute method.

AttributeError: 'coroutine' object has no attribute 'execute'

It gives me this error


Solution

  • asyncpg.connect is a async method, just edit to

    await asyncpg.connect(r"postgresql://postgres@localhost/user")