Re: Facing error trying to pull out data from column

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: Shaan Repswal <shaun(dot)reapswaal(at)gmail(dot)com>, psycopg(at)postgresql(dot)org
Subject: Re: Facing error trying to pull out data from column
Date: 2016-06-05 02:30:19
Message-ID: 7597d61a-6bca-9d4c-638e-4c5ee7d7fed1@aklaver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

On 06/04/2016 06:32 PM, Shaan Repswal wrote:
> 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?

Read this:

http://initd.org/psycopg/docs/usage.html#passing-parameters-to-sql-queries

in particular:

The problem with the query parameters

>
> What am I doing wrong here?

--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com

In response to

Responses

Browse psycopg by date

  From Date Subject
Next Message Jim Nasby 2016-06-06 21:29:08 Re: Facing error trying to pull out data from column
Previous Message Shaan Repswal 2016-06-05 01:32:32 Facing error trying to pull out data from column