Django找不到PostgreSQL数据库中存在的表

时间:2019-02-26 02:52:35

标签: django postgresql

我是数据库编程的新手,很抱歉,如果我简单地问一些问题。

我最近使用Django模型和迁移功能向数据库中添加了一些表,现在我正在使用python带来数据并在脚本上打印

现在要指出我的错误:

DB is connected successfully
Failed to execute database program
relation "cgi_limit" does not exist
LINE 1: SELECT * FROM CGI_limit
                      ^
connection of DB had close successfully

现在我对命名进行了两次检查。我尝试使用其他表,例如auth_user,它能够打印表内容,并检查表是否在数据库中退出,如下所示;

Farm=# SELECT * FROM pg_tables;
         schemaname     |         tablename          | tableowner | tablespace | hasindexes | hasrules | hastriggers | rowsecurity
     public             | django_session             | FAT        |            | t          | f        | f           | f
     public             | auth_permission            | FAT        |            | t          | f        | t           | f
     public             | auth_user_user_permissions | FAT        |            | t          | f        | t           | f
     public             | auth_user                  | FAT        |            | t          | f        | t           | f
     public             | django_admin_log           | FAT        |            | t          | f        | t           | f
     public             | CGI_ambient                | FAT        |            | t          | f        | f           | f
     public             | CGI_tank_system            | FAT        |            | t          | f        | f           | f
     public             | CGI_limit                  | FAT        |            | t          | f        | f           | f

我是呈现数据库的python代码;

#import liberys 
import psycopg2 as pg2
from datetime import timedelta, datetime, date

############################################
# Function codes
def getDbConnection():
    #Get Database connection
    try:
        connection =pg2.connect(user='FAT',
                             password='*******',
                             host='',
                             port='5432',
                             database='Farm') 
        print ("DB is connected succefully")
        return connection
    except(Exception, pg2.DatabaseError) as error:
        print("Failed to connect to database")

def closeDbConnection(connection):
    #Close Database connection
    try:
        connection.close()
        print("connection of DB had close succefully")
    except(Exception, pg2.DatabaseError) as error:
        print("Failed to close database connection")

def DisplayDBdata():
    try:
        connection = getDbConnection()
        cursor = connection.cursor()
        query = 'SELECT * FROM "CGI_limit"' 

        cursor.execute(query,)
        records = cursor.fetchall()

        for row in records:
            print("date: = ", row[1])

    except(Exception, pg2.DatabaseError) as error:
        print("Failed to execute database program")
        print(error)


    finally:
            closeDbConnection(connection)


#############################################################
#code to be excuted
#DeleteDBdata()
DisplayDBdata() #for testing only

#end of code thats excute

我为应该做的事感到沮丧。我做了一些谷歌搜索,结果只命名

如果您能帮助我

1 个答案:

答案 0 :(得分:3)

Postgres不喜欢大写的表名。您需要将表名放在引号中以使其起作用。我建议使用小写字母。

query = 'SELECT * FROM "CGI_limit"'

Documentation link