首页 > 解决方案 > psycopg2 找不到表

问题描述

我有一个 Django 休息框架 API。

我正在尝试使用 PostgreSQL 和 psycopg2 从中访问一些数据。

import psycopg2

try:
    connection = psycopg2.connect(user="*****",
                                  password='*****',
                                  host="127.0.0.1",
                                  port="5432",
                                  database="locations")
    cursor = connection.cursor()
    postgreSQL_select_Query = "select * from KnownLocations"

    cursor.execute(postgreSQL_select_Query)
    print("Selecting rows from find_second_gdt_points")
    records = cursor.fetchall()
    print(records)

except (Exception, psycopg2.Error) as error:
    print("Error while fetching data from postgreSQL", error)

finally:
    # Closing connection
    if connection:
        cursor.close()
        connection.close()
        print("Connection to DB is closed.")

运行脚本时出现以下错误:

Error while fetching data from PostgreSQL relation "knownlocations" does not exist
LINE 1: select * from KnownLocations

但是在终端中打印数据库时,该表确实存在。

locations=# \dt

                     List of relations
 Schema |               Name               | Type  | Owner 
--------+----------------------------------+-------+-------
 public | KnownLocations                   | table | yovel
 public | auth_group                       | table | yovel
 public | auth_group_permissions           | table | yovel
 public | auth_permission                  | table | yovel
 public | auth_user                        | table | yovel
 public | auth_user_groups                 | table | yovel
 public | auth_user_user_permissions       | table | yovel
 public | authtoken_token                  | table | yovel
 public | django_admin_log                 | table | yovel
 public | django_content_type              | table | yovel
 public | django_migrations                | table | yovel
 public | django_session                   | table | yovel
 public | find_second_gdt_points           | table | yovel
 public | threelocationstrian_locationinfo | table | yovel
(14 rows)

标签: pythonpostgresqlpsycopg2

解决方案


推荐阅读