Обсуждение: Facing error trying to pull out data from column
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())
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')
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?
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
On 6/4/16 9:30 PM, Adrian Klaver wrote: >> 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? Another suggestion: build your column list once, and run ONE query, not hundreds or thousands. You'll get MUCH better performance. -- Jim Nasby, Data Architect, Blue Treble Consulting, Austin TX Experts in Analytics, Data Architecture and PostgreSQL Data in Trouble? Get it in Treble! http://BlueTreble.com 855-TREBLE2 (855-873-2532) mobile: 512-569-9461