Ich möchte meinen Query Builder nicht ändern. Mein Abfragebauer gibt eine SQL -Abfrage als Zeichenfolge aus (in Wirklichkeit gibt es tatsächlich eine Abfrage mit '%s' aus, um Parameter zu entkommen, und die entkommenen Parameter in einer Liste). ?
Code: Select all
import asyncio
import psycopg
class MyQueryBuilderObj:
def getFinalQuery(self) -> str:
return "SELECT * FROM test_table"
async def fetch_data_from_db():
qb_obj = MyQueryBuilderObj()
async with await psycopg.AsyncConnection.connect("dbname=test user=postgres password=yourpassword host=localhost") as aconn:
async with aconn.cursor() as acur:
await acur.execute(qb_obj.getFinalQuery())
result = await acur.fetchone()
return result
async def main():
try:
result = await fetch_data_from_db()
print("Fetched data:", result)
except Exception as e:
print("An error occurred:", e)
if __name__ == "__main__":
asyncio.run(main())
< /code>
Ausgabe unter "acur.execute": < /p>
Argument of type "str" cannot be assigned to parameter "query" of type "Query" in function "execute"
Type "str" is not assignable to type "Query"
"str" is not assignable to "LiteralString"
"str" is not assignable to "bytes"
"str" is not assignable to "SQL"
"str" is not assignable to "Composed"