gpt4 book ai didi

Python Psycopg 执行脚本方法

转载 作者:行者123 更新时间:2023-11-29 13:15:18 35 4
gpt4 key购买 nike

sqlite 的 Python db-api 实现有一个方便的方法 executescript() 来执行多语句 SQL 脚本。它对于创建数据库非常有用。参见 sqlite driver documentation .

我找不到类似的用于 PostgreSQL 的 Psycopg db-api 驱动程序。Psycopg 中是否存在 executescript()?还有其他选择吗?

谢谢

最佳答案

您不需要它——单个查询字符串可以已经包含多个单独的查询。

引自the wire protocol documentation , 强调:

A simple query cycle is initiated by the frontend sending a Query message to the backend. The message includes an SQL command (or commands) expressed as a text string. The backend then sends one or more response messages depending on the contents of the query command string, and finally a ReadyForQuery response message. ReadyForQuery informs the frontend that it can safely send a new command. (It is not actually necessary for the frontend to wait for ReadyForQuery before issuing another command, but the frontend must then take responsibility for figuring out what happens if the earlier command fails and already-issued later commands succeed.)

...及以后:

Since a query string could contain several queries (separated by semicolons), there might be several such response sequences before the backend finishes processing the query string. ReadyForQuery is issued when the entire string has been processed and the backend is ready to accept a new query string.


因此,可以为标准的 cursor.execute() 调用提供一个以分号分隔的要运行的单个查询列表。

关于Python Psycopg 执行脚本方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50005663/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com