0

I am new to postgresql and python. Could you please help me to fix this issue. I am getting undefined column error while fetching with where caluse. PostgreSQL 14 and python 3.8 version has been used.

import psycopg2

con = psycopg2.connect(database="dbname",user="postgres",password="pwd",host="localhost",port= '5432')
cursor_obj = con.cursor()
#print(s3.x)
x = 'select "*" FROM PUBLIC."tab1" '
y = 'where "SSN"="123-45-6789" '
#z = '"123-45-6789"'
a = x + y
cursor_obj.execute(a)    

#cursor_obj.execute('SELECT "SSN" FROM PUBLIC."tab1" where "SSN" = "123-45-6789 "')    
result = cursor_obj.fetchall()
print("Result set:","\n",result)
con.commit()
con.close()
ere

Error: UndefinedColumn: column "123-45-6789" does not exist LINE 1: ...NFD-SSN" FROM PUBLIC."tab1" where "SSN"="123-45-67...

I tried changing the apostrophes but still didn't work

  • The problem is that "123-45-6789" is being recognised as the name of the column itself, rather than an entry in a column. – Michael Grogan Apr 04 '23 at 14:10
  • 1
    Which apostrophes did you change, what did you change them to, and what happened? ("didn't work" is not a description, it is an excuse provided in lieu of a description) – jjanes Apr 04 '23 at 14:55
  • 1) Read up on Python [Triple quoting](https://stackoverflow.com/questions/23361171/triple-quotation-in-python). 2) In Postgres "" means you are writing an identifier, a table or column name. 3) Is `SSN` actually all upper case in the table definition? 4) Read up on [Passing parameters](https://www.psycopg.org/docs/usage.html#passing-parameters-to-sql-queries). – Adrian Klaver Apr 04 '23 at 15:35
  • Thanks all. https://stackoverflow.com/questions/70621488/python-find-a-string-with-single-quote-symbol-in-postgresql-table post helped mt to fix the issue. – Murali Kumar Apr 04 '23 at 16:29
  • Unrelated to your problem, but: Postgres 95 and 9.4 are [no longer supported](https://www.postgresql.org/support/versioning/) you should plan an upgrade as soon as possible. –  Apr 04 '23 at 17:54
  • I hope that does not mean you used `f` string formatting to build your query. – Adrian Klaver Apr 04 '23 at 18:31

0 Answers0