如何在执行“显示”数据库时求解psycopg语法询问?
我使用Windows 11,Python 3.10.4 64位,PostgreSQL 14.2数据库和Psycopg2 2.9.3。
我连接到数据库:
import psycopg2
SCHEMA_NAME = "myschema"
connx = psycopg2.connect(database="mydatabase", user="myadmin", password="123456", host="127.0.0.1", port="5432", options="-c search_path=myschema")
cur = connx.cursor()
然后我想检查模式是否存在:
sql_str = "SHOW DATABASES LIKE %s ESCAPE ''"
cur.execute(sql_str, [SCHEMA_NAME])
但是我会收到以下错误:
Traceback (most recent call last):
cur.execute("SHOW `DATABASES` LIKE %s ESCAPE ''", [SCHEMA_NAME])
psycopg2.errors.SyntaxError: syntax error at or near "LIKE"
LINE 1: SHOW DATABASES LIKE 'myschema' ESCAPE ''
这里有什么问题?
I use Windows 11, Python 3.10.4 64-bit, PostgreSQL 14.2 database and psycopg2 2.9.3.
I connect to a database:
import psycopg2
SCHEMA_NAME = "myschema"
connx = psycopg2.connect(database="mydatabase", user="myadmin", password="123456", host="127.0.0.1", port="5432", options="-c search_path=myschema")
cur = connx.cursor()
Then I want to check if the schema exists:
sql_str = "SHOW DATABASES LIKE %s ESCAPE ''"
cur.execute(sql_str, [SCHEMA_NAME])
but I get the following error:
Traceback (most recent call last):
cur.execute("SHOW `DATABASES` LIKE %s ESCAPE ''", [SCHEMA_NAME])
psycopg2.errors.SyntaxError: syntax error at or near "LIKE"
LINE 1: SHOW DATABASES LIKE 'myschema' ESCAPE ''
What is wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在Postgresql A 架构中和A 数据库是2种不同的方法。
以下查询将告诉您该模式是否存在。
并且您的
如果您正在检查是否存在数据库,
代码将变为
In PostgreSQL a schema and a database are 2 different things.
The following query will tell you whether the schema exists.
And your code becomes
If you are checking that a database exists it will be
And your code will become