1

I have a table populated with values, one being date in the YYYY-MM-DD format. I'm trying to print all "cost" values by month. So,

def SumByMonth():
    usrDate = raw_input('Enter month: ')
    sql = '''SELECT cost FROM Finance WHERE strftime('%m', date) = ?''', (usrDate)
    month_cost = [t[0] for t in cur.execute(sql)]
    print month_cost

This code gives me this error:

ValueError: operation parameter must be str or unicode

So I figure the SQL command isn't actually extracting anything? What am I missing here?

jason
  • 113
  • 3
  • 10

1 Answers1

0

There is a problem with line

sql = '''SELECT cost FROM Finance WHERE strftime('%m', date) = ?''', (usrDate)

You need to write SQL query in string or unicode format like the error states

Here is a correct code :

def SumByMonth():
    usrDate = raw_input('Enter month: ')
    month_cost = [t[0] for t in cur.execute("SELECT cost FROM Finance WHERE strftime('%m', date) = ?", (usrDate,))]
    print month_cost
Abhijeet Kasurde
  • 3,937
  • 1
  • 24
  • 33