我试图弄清楚为什么我无法使用psycopg2访问PostgreSQL数据库中的特定表。我正在运行PostgreSQL 11.5
如果这样做,我可以连接到有问题的数据库并读取其中的所有表:
import psycopg2
try:
connection = psycopg2.connect(user = "postgres", #psycopg2.connect() creates connection to PostgreSQL database instance
password = "battlebot",
host = "127.0.0.1",
port = "5432",
database = "BRE_2019")
cursor = connection.cursor() #creates a cursor object which allows us to execute PostgreSQL commands through python source
#Print PostgreSQL version
cursor.execute("""SELECT table_name FROM information_schema.tables
WHERE table_schema = 'public'""")
for table in cursor.fetchall():
print(table)
结果如下:
('geography_columns',)
('geometry_columns',)
('spatial_ref_sys',)
('raster_columns',)
('raster_overviews',)
('nc_avery_parcels_poly',)
('Zone5e',)
('AllResidential2019',)
#....etc....
我感兴趣的表是最后一个'AllResidential2019'
所以我尝试通过以下步骤连接到它并打印内容:
try:
connection = psycopg2.connect(user = "postgres",
#psycopg2.connect() creates connection to PostgreSQL database instance
password = "battlebot",
host = "127.0.0.1",
port = "5432",
database = "BRE_2019")
cursor = connection.cursor() #creates a cursor object which allows us to execute PostgreSQL commands through python source
cursor.execute("SELECT * FROM AllResidential2019;") #Executes a database operation or query. Execute method takes SQL query as a parameter. Returns list of result
record = cursor.fetchall()
print(record)
except (Exception, psycopg2.Error) as error:
print("Error while connecting to PostgreSQL: ", error)
然后出现以下错误:
Error while connecting to PostgreSQL: relation "allresidential2019" does not exist
LINE 1: SELECT * FROM AllResidential2019;
但是,当我尝试连接到我拥有的另一个数据库中的另一个表时,我可以成功连接并获得结果(这可行!结果是该表中的数据):
try:
connection = psycopg2.connect(user = "postgres", #psycopg2.connect() creates connection to PostgreSQL database instance
password = "battlebot",
host = "127.0.0.1",
port = "5432",
database = "ClimbingWeatherApp") . #different database name
cursor = connection.cursor()
cursor.execute("SELECT * FROM climbing_area_info ;")
record = cursor.fetchall()
print(record)
except (Exception, psycopg2.Error) as error:
print("Error while connecting to PostgreSQL: ", error)
我不知道为什么我可以使用完全相同的代码从一个表中检索信息,而不能从另一表中检索信息(除了名称是更改之外)。而且我也不确定如何解决此问题。谁能提供建议?
答案 0 :(得分:2)
您的表名区分大小写,必须用双引号将其关闭:
SELECT * FROM "AllResidential2019";
在Python程序中,它可能看起来像这样:
cursor.execute('SELECT * FROM "AllResidential2019"')
或者您可以使用专用模块SQL string composition:
from psycopg2 import sql
# ...
cursor.execute(sql.SQL("SELECT * FROM {}").format(sql.Identifier('AllResidential2019')))
请注意,区分大小写的Postgres标识符(即表,列,视图,函数等的名称)不必要地使简单问题复杂化。我建议您不要使用它们。
答案 1 :(得分:1)
同样,您遇到问题的原因是Postgres的报价规则,该规则遵循有关双引号标识符的ANSI SQL标准。在创建表时,您可能会引用该表:
CREATE TABLE "AllResidential2019" (
...
)
由于至少一个大写字母区分大小写,因此要求您在引用表格时始终引用表格。请记住:SQL中的单引号和双引号具有不同的含义,而不是在Python中通常可以互换。
SELECT * FROM "AllResidential2019"
DELETE FROM "AllResidential2019" ...
ALTER TABLE "AllResidential2019" ...
如果表,列或其他标识符不包含特殊字符,空格或reserved words,通常建议始终使用小写字母或不使用引号:
CREATE TABLE "allresidential2019" (
...
)
CREATE TABLE AllResidential2019 (
...
)
这样做,任何大写字母组合都可以使用
SELECT * FROM ALLRESIDENTIAL2019
SELECT * FROM aLlrEsIdEnTiAl2019
SELECT * FROM "allresidential2019"
请参阅有关该主题的更多阅读材料