I'm trying to create a simple cache using SQLite for certain queries against WolframAlpha's API in Python. The problem is that the SQLite INSERT fails for certain queries, and I really don't know why.
At http://products.wolframalpha.com/api/explorer.html it is possible to run exactly the two queries I ran. They are:
nutritional information of coke
and
nutritional information of milk
The INSERT for the first query works great, but the second fails.
Here's the code that takes care of the caching functionality:
def run_query(input_query):
input_query = query_stub + input_query
cursor.execute('SELECT response FROM cache WHERE query=?', (input_query,))
response = cursor.fetchone()
if response:
print " Cache hit on: %s" % (input_query)
response = response[0]
else:
print " Cache miss on: %s" % (input_query)
query = waeo.CreateQuery(input_query)
print '1'
response = waeo.PerformQuery(query)
print '2'
cursor.execute('INSERT INTO cache VALUES (?, ?)', (input_query, response,))
print '3'
conn.commit()
print '4'
output_json = xmltodict.parse(response)
return jsonify(output_json)
The table is defined as:
cursor.execute('CREATE TABLE cache (query text, response text)')
And here are part of the logs, so as you can see, the INSERT is failing:
* Running on http://0.0.0.0:8080/
Cache miss on: nutritional information of coke
1
2
3
4
127.0.0.1 - - [20/Sep/2013 17:51:16] "GET /wa?item=coke HTTP/1.1" 200 -
Cache miss on: nutritional information of milk
1
2
127.0.0.1 - - [20/Sep/2013 17:51:47] "GET /wa?item=milk HTTP/1.1" 500 -