我有这个 super 简单的 Python 代码来连接到 Postgres 并创建一个模式。它没有显示错误,但我没有看到在 Postgres 中添加了架构。我一定错过了一些显而易见的东西。
我什至不确定这是否连接到 Postgres,因为当我给它一个错误的密码时,它仍然没有错误地返回。但是,当我反复试验时,它一直说连接已到位。请帮忙~谢谢!
import psycopg2
psycopg2.autocommit = True
def build_archive(db1):
db1 = psycopg2.connect(database='postgres', user='operator', password='1234', host='localhost', port='2280')
cursor = db1.cursor()
sql = """CREATE SCHEMA app_tools AUTHORIZATION operator;"""
cursor.execute(sql)
最佳答案
在您的代码中没有实际调用 build_archive() 的地方,因此永远不会执行其中的代码。您需要在函数定义下方添加对 build_archive() 的调用,然后您将开始看到输出。
例如:
import psycopg2
psycopg2.autocommit = True
def build_archive(db1):
db1 = psycopg2.connect(database='postgres', user='operator',
password='1234', host='localhost', port='2280')
cursor = db1.cursor()
sql = """CREATE SCHEMA app_tools AUTHORIZATION operator;"""
cursor.execute(sql)
build_archive() # Add this call to build_archive()
要了解有关如何在 Python 中使用函数的更多信息,请阅读 here .
关于在 Postgres 中创建模式的 Python 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45381870/