Home > Mobile >  PostgreSQL Error while executing sql command in Python
PostgreSQL Error while executing sql command in Python

Time:10-16

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',)

CodePudding user response:

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.

  • Related