0

I'm running a flask web framework tied in to a sqlite databade. I'm having an issue returning a query that includes both the column name and the value of that column. I'm including the relevant code below.

import threading
import time
import KeySys
import serial
from flask import Flask, request
from flask_restful import reqparse, abort, Api, Resource
from sqlalchemy import create_engine
from json import dumps
from time import sleep
from datetime import datetime

e = create_engine('sqlite:///lockdb.db')
app = Flask(__name__)
api = Api(app)
parser = reqparse.RequestParser()
parser.add_argument('LockSwitch')

class Keystat_Meta(Resource):
    def get(self):
        #Connect to databse
        conn = e.connect()
        #Perform query and return JSON data
        query = conn.execute('select * from LockStat')
        return {'KeyStat': [query.cursor.fetchall()]} # I've tried keys() all() items(column, value) and various different queries.

api.add_resource(Keystat_Meta, '/keystat')

if __name__ == '__main__':
    app.run()
4
  • 1
    What error are you getting? query should be a dictionary according to the sqlalchemy docs. Try printing query.items() to see if you're getting results. Commented Dec 14, 2016 at 1:07
  • Thank you for helping, when I do query.cursor.fetchall() I get the values without the column names, if I do query.keys() I get all the column names but no values. if I do query.items() I get the error 'ResultProxy' object has no attribute 'items' Commented Dec 14, 2016 at 1:10
  • You may refer to this thread stackoverflow.com/questions/22277548/… Commented Dec 14, 2016 at 3:13
  • @ichbinblau That documentation is for sqlalchemy .9 and is deprecated. I'm running the latest sqlalchemy 1.1. I tried it anyways but no it doesn't work column_description is no longer a query option. Commented Dec 14, 2016 at 18:55

1 Answer 1

1

If anyone else is struggling with this, it took me a day of pounding at it but I found a solution

return {'KeyStat': [dict(zip(tuple (query.keys()) ,i)) for i in query.cursor]}

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.