# This program connects to sqlite3 and executes queries. import sqlite3 db = sqlite3.connect('z:\db\mydata.db') cursor = db.cursor() # Now, write your loop below. lower = 60 upper = 69 fh = open('z:\db\sheet.xls','w') # Write the the title of each column of the table print "Run Time\t\t\tfilms\n" fh.write("Run Time\tfilms\n") # Our for loop for i in range(18): # Define your query as a string and assign it to the variable query query = '''SELECT COUNT(distinct id) FROM Movie WHERE runtime BETWEEN ? AND ?;''' cursor.execute(query, [lower, upper]) # you will need the following for getting the query results result = cursor.fetchone() print "Between",lower,"-",upper,"minutes \t",result[0] # write to excel file line = "Between " + str(lower) + " - " + str(upper) + " minutes \t" + str(result[0]) + "\n" fh.write(line) # increment lower and upper lower = lower + 10 upper = upper + 10 fh.close() db.commit() db.close()