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.klaver@aklaver.com