小编Rem*_*007的帖子

使用 Asyncpg 使用“executemany”将 json 数据插入表中

我想使用 Asyncpg 在表中插入一些 json 数据(2 列: id 、 cluster_json )。我想使用“executemany”函数来加速插入过程。

我的代码:

async def main():

    conn = await asyncpg.connect('postgresql://postgres:postgres@localhost:5432/postgres')
    statement = '''INSERT INTO cluster(cluster_json) VALUES($1) '''
    await conn.executemany(statement, [{"name":"John", "age":30, "car":null},
                                       {"name":"John1", "age":31, "car":null}'])

    await conn.close()

asyncio.get_event_loop().run_until_complete(main())
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误:

asyncpg.exceptions.DataError: invalid input in executemany() argument sequence element #0: expected a sequence, got dict
Run Code Online (Sandbox Code Playgroud)

我试图将字典作为 str 传递。也有一个错误。

错误消息很清楚,代码与文档中的代码非常相似,
期望我想要插入 json 数据。不幸的是,我没有看到我错过了什么。有人发现问题/帮助我吗?提前致谢。

python postgresql asyncpg

2
推荐指数
1
解决办法
820
查看次数

标签 统计

asyncpg ×1

postgresql ×1

python ×1