PostgreSQL Table 存在,但 python 表示不存在
PostgreSQL Table exists but python says it doesn't
我是 Python 的新手,虽然这看起来像是一些愚蠢的错误,但我无法弄清楚我做错了什么。任何建议或提示都会非常有帮助。另外,如果这个问题之前已经回答过,请 link 我回答一下。
我正在编写一个简单的 python 脚本,它将连接到数据库。现在在该数据库中,我正在检查 test_run
table 是否存在。如果它存在就打印它存在,如果不存在,创建 table.
# Connect to the Database Server, now with the created DB
try:
connection = psycopg2.connect(user="postgres", password="postgres", host="127.0.0.1", port="5432",
database="dashboard")
except (Exception, psycopg2.Error) as error:
print("Connection not established", error)
# Check if test_run Table Exists
cursor = connection.cursor()
if bool(cursor.execute("SELECT EXISTS(SELECT * FROM information_schema.tables WHERE table_name='test_run')")):
print('test_run table exists. Moving On.')
else:
print('test_run does not exist. Creating the Table now.')
cursor.execute("CREATE TABLE test_run (run_id serial PRIMARY KEY, date date, status varchar(255), "
"total_time integer, project_name varchar(255));")
现在在数据库中 table test_run
存在,当我 运行 SELECT EXISTS
命令时,我得到一个 true
.
现在,当我 运行 执行上述脚本 else
条件时,表明 test_run
table 不存在。但理想情况下,if
条件应该在 table 确实存在时执行。
你应该试试这个。
import psycopg2
# Connect to the Database Server, now with the created DB
try:
connection = psycopg2.connect(user="postgres", password="postgres", host="127.0.0.1", port="5432", database="dashboard")
except (Exception, psycopg2.Error) as error:
print("Connection not established", error)
# Check if test_run Table Exists
cursor = connection.cursor()
cursor.execute("SELECT EXISTS(SELECT * FROM information_schema.tables WHERE table_name='test_run')")
if bool(cursor.fetchone()[0]):
print('test_run table exists. Moving On.')
else:
print('test_run does not exist. Creating the Table now.')
cursor.execute("CREATE TABLE test_run (run_id serial PRIMARY KEY, date date, status varchar(255), "
"total_time integer, project_name varchar(255));")
connection.commit()
我是 Python 的新手,虽然这看起来像是一些愚蠢的错误,但我无法弄清楚我做错了什么。任何建议或提示都会非常有帮助。另外,如果这个问题之前已经回答过,请 link 我回答一下。
我正在编写一个简单的 python 脚本,它将连接到数据库。现在在该数据库中,我正在检查 test_run
table 是否存在。如果它存在就打印它存在,如果不存在,创建 table.
# Connect to the Database Server, now with the created DB
try:
connection = psycopg2.connect(user="postgres", password="postgres", host="127.0.0.1", port="5432",
database="dashboard")
except (Exception, psycopg2.Error) as error:
print("Connection not established", error)
# Check if test_run Table Exists
cursor = connection.cursor()
if bool(cursor.execute("SELECT EXISTS(SELECT * FROM information_schema.tables WHERE table_name='test_run')")):
print('test_run table exists. Moving On.')
else:
print('test_run does not exist. Creating the Table now.')
cursor.execute("CREATE TABLE test_run (run_id serial PRIMARY KEY, date date, status varchar(255), "
"total_time integer, project_name varchar(255));")
现在在数据库中 table test_run
存在,当我 运行 SELECT EXISTS
命令时,我得到一个 true
.
现在,当我 运行 执行上述脚本 else
条件时,表明 test_run
table 不存在。但理想情况下,if
条件应该在 table 确实存在时执行。
你应该试试这个。
import psycopg2
# Connect to the Database Server, now with the created DB
try:
connection = psycopg2.connect(user="postgres", password="postgres", host="127.0.0.1", port="5432", database="dashboard")
except (Exception, psycopg2.Error) as error:
print("Connection not established", error)
# Check if test_run Table Exists
cursor = connection.cursor()
cursor.execute("SELECT EXISTS(SELECT * FROM information_schema.tables WHERE table_name='test_run')")
if bool(cursor.fetchone()[0]):
print('test_run table exists. Moving On.')
else:
print('test_run does not exist. Creating the Table now.')
cursor.execute("CREATE TABLE test_run (run_id serial PRIMARY KEY, date date, status varchar(255), "
"total_time integer, project_name varchar(255));")
connection.commit()