Python Psycopg 执行脚本方法
Python Psycopg executescript method
sqlite 的 Python db-api 实现有一个方便的方法 executescript() 来执行多语句 SQL 脚本。
它对于创建数据库非常有用。
参见 sqlite driver documentation。
我找不到类似的 Psycopg db-api PostgreSQL 驱动程序。
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()
调用提供以分号分隔的单个查询列表 运行。
sqlite 的 Python db-api 实现有一个方便的方法 executescript() 来执行多语句 SQL 脚本。 它对于创建数据库非常有用。 参见 sqlite driver documentation。
我找不到类似的 Psycopg db-api PostgreSQL 驱动程序。 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()
调用提供以分号分隔的单个查询列表 运行。