所以我正在使用 asyncpg 准备好的语句并执行查询。我在查询中传递位置参数。
目前,我通过了,stmt.cursor(params[0], params[1])
但如果我不知道传递了多少个参数,所以想将一个列表或其他东西传递给这个方法。在这种情况下,我怎样才能做到这一点?
我当前的代码:
async def execute_ps(dsn):
conn = await asyncpg.connect(dsn)
sql_query = """select * from table1 where id = $1 and name=$2"""
params = [var1, var2]
stmt = await conn.prepare(sql_query)
result = []
try:
async with conn.transaction():
async for record in stmt.cursor(params[0], params[1]): # want to pass list here
print(record)
result.append(record)
except Exception as e:
print("exception: {}", e)
if conn is None:
await conn.close()
return result