python - psycopg2 - Postgresql 数据库中的 'Relation does not exist'

标签 python python-3.x postgresql psycopg2

我想弄清楚为什么我无法使用 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)

我不明白为什么我可以使用完全相同的代码(除了名称发生变化)从一个表中检索信息,而不能从另一个表中检索信息。而且我也不确定如何解决这个问题。谁能提供建议?

最佳答案

您的表名区分大小写,您必须用双引号将其括起来:

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 标识符(即表、列、 View 、函数等的名称)不必要地使简单的事情复杂化。我建议您不要使用它们。

关于python - psycopg2 - Postgresql 数据库中的 'Relation does not exist',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59197718/

相关文章:

java - JPQL Eclipselink - 如何连接两个表并显示另一个表中没有项目的项目

python - 适用于 windows 和 postgres 8 的 psycopg2 二进制包?

java - Android 无法直接连接 Postgres 数据库,也无法使用 JDBC 连接

python - Python "open"函数是将其内容保存在内存中还是临时文件中?

python - 为所有Flask路线添加前缀

python - 无法解析来自 xml 内容的链接

python - 按字符拆分泰语文本

python - 标准库中的所有内容都会在 Python 3.0 中将字符串视为 unicode 吗?

python - 使用 Tornado 通过 websocket 发送 cookie

python - 如何将字典转换为多级数据框?