0

Using flask-restful, and having a database which stores, related to places, the name, hour of visit, hour of exit, and date of visit, I have this query:

query = conn.execute("select * from geo_example where     init_hour='%d'"%hour_of_visit)

How do I use the jsonify function instead of doing it manually like this:

result = {'inital_hour': [dict(zip(tuple (query.keys()) ,i)) for i in     query.cursor]}
Shoplifter20
  • 151
  • 2
  • 2
  • 12
  • 1
    I'd start with [sqlalchemy](http://www.sqlalchemy.org/) and [flask-sqlalchemy](http://flask-sqlalchemy.pocoo.org/2.1/) libraries to abstract database functionality. You're working at a very low level (raw SQL), which is a pain, and seems unnecessary here. Moreover, it's hard to tell what your query outputs right now, but assuming you created a list you want to return from your route, a simple `return jsonify(result={'initial_hour':MyList})` should work fine. – GG_Python Jan 28 '16 at 15:22
  • I'm simply returning that result, with the output like { "inital_hour": [ { "Date": "2016-01-10", "End_Hour": 900, "Init_Hour": 1800, "Name": "HOME" } ] } – Shoplifter20 Jan 28 '16 at 15:29
  • the `return jsonify(..)` i've posted above should work for you then. What is failing, if anything? – GG_Python Jan 28 '16 at 15:31
  • And for what I am trying to achieve here, raw SQL makes ir easier for me. Maybe in the future I'll use other libraries. – Shoplifter20 Jan 28 '16 at 15:34
  • I've written this `query = conn.execute("select * from geo_example") MyList = list (i[0] for i in query.cursor.fetchall()) return jsonify ({'places':MyList})` It works wonderfully. Many thanks! – Shoplifter20 Jan 28 '16 at 15:35

0 Answers0