1

I'm stuck on this error running the script below which goes through each column in my table and returns the max price for each. n.b. I've shortened the amount columns for this question.

import sqlite3

sqlite_file = 'foo.db'
conn = sqlite3.connect(sqlite_file)
c = conn.cursor()
column = ['AAC', 'AAD', 'ABC', 'ABP', 
          'AGL', 'AHG', 'AHY', 'AIO', 
          'ALL', 'ALQ', 'ALU', 'AMC']
for i in range(len(column)):
    c.execute("SELECT max(%s) FROM prices" % (column[i]))
    print (column[i], c.fetchall())
conn.close()

The script prints 8 results then I get the error?

Output:

AAC [('1.495',)]
AAD [('2.50',)]
ABC [('4.455',)]
ABP [('2.98',)]
AGL [('16.86',)]
AHG [('4.25',)]
AHY [('1.70',)]
AIO [('8.75',)]
sqlite3.OperationalError: wrong number of arguments to function max()

Please help!

1 Answer 1

1

ALL is a keyword in SQLite. (https://www.sqlite.org/lang_keywords.html)

You need to quote it to make your query run correctly (see the link above).

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Szymon I would've been stuck on that for hours. I used c.execute("SELECT max("'"%s"'") FROM prices" % (columns[i]))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.