简体   繁体   中英

PostgreSQL Error while executing sql command in Python

i've been trying to get some data from my db by using below code, but the code is not working. is there any mistake that i made in the code, if so how can i fix it.

NOTE: i took the below code from just a script not a django or flesk web app.

def db():
    conn = psycopg2.connect(
        "dbname=mydb user=postgres password=****** host=*.*.*.*")
    cur = conn.cursor()
    cur.execute("""SELECT * FROM MddPublisher""")
    query_results = cur.fetchall()
    print(query_results)


db()

ERROR: psycopg2.errors.UndefinedTable: relation "mddpublisher" does not exist LINE 1: SELECT * FROM MddPublisher

additionally,i want to show below code to prove that connection is ok. the problem is that i can't receive data from my db whenever i try to execute select command through python.

def print_tables():
    conn = psycopg2.connect(
        "dbname=mydb user=postgres password=***** host=*.*.*.*.*")
    cur = conn.cursor()
    cur.execute("""SELECT table_name FROM information_schema.tables
       WHERE table_schema = 'public'""")
    for table in cur.fetchall():
        print(table)
print_tables()

OUTPUT:

('MddPublisher',)

This is probably an issue with case sensitivity. Postgresql names are usually normalized to lower case. However, when used inside double quotes, they keep their case. So, to access a table named MddPublisher you must write it like "MddPublisher ".

All the gory details are in Section 4.1.1, Identifiers and Key Words in the Postgresql 14 docs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM