Facing error trying to pull out data from column

From: Shaan Repswal <shaun(dot)reapswaal(at)gmail(dot)com>
To: psycopg(at)postgresql(dot)org
Subject: Facing error trying to pull out data from column
Date: 2016-06-05 01:32:32
Message-ID: CALa+hEQRybA8C7H7ZSZg4z83UGYR0AG83RNcwkSzCdoMSMMKLw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

So here is my double loop:-

for col in colnames[3:]:
for name in names:
query = "select format('SELECT %I FROM inventory WHERE name = {},
'{}')".format(name, col)
cur.execute(query) #This is line 18
query_str = cur.fetchone()[0]
cur.execute(query_str)
print(cur.fetchone())

And this is the error it is giving me:-

Traceback (most recent call last):
File "C:\Python34\psycopg2\printing colnames.py", line 18, in <module>
cur.execute(query)
psycopg2.ProgrammingError: syntax error at or near "25"
LINE 1: ...ventory WHERE name = Polyester Direct High Gloss, '25 LTRS')

Help me out folks.

My table columns are like this :-
name code type '25 LTRS' '12 LTRS' '26.2 LTRS' '500 ML'.... and so on...

And I want to print and cycle through all of the LTRS/ML (quantity columns)
for every product name that I have. How do I go about that?

What am I doing wrong here?

Responses

Browse psycopg by date

  From Date Subject
Next Message Adrian Klaver 2016-06-05 02:30:19 Re: Facing error trying to pull out data from column
Previous Message Daniele Varrazzo 2016-05-24 10:58:29 Re: Pass parameters to cursor.execute('Listen ')?