在json输出中获取查询执行时间

时间:2017-07-25 08:35:22

标签: python flask flask-restful

from flask import Flask, request
import src.dbcon as db
app = Flask(__name__)
@app.route("/", methods=['POST'])
def hello():
    id = request.args.get('id')
    query = "select * from employee where id= {id} limit 2;"
    with db.sample(query.format(id=request.args.get('id'))) as data:
        return data
  am returning data in json format.

1)我必须返回查询执行时间和json数据。    sample:" query_execution_time“:2345, 2)在json输出中我得到一些空隙  [{" id":" 10000"," roll_no":" 02V}] 那么如何去除这些空间?

def getcursor_read(query): 
    a=read_create_conn(config=configuration) 
    print("Created read connection,") 
    con = a.getconn() 
    print(query) 

    try: 
        cur = con.cursor(cursor_factory=psycopg2.extras.DictCursor) 
        cur.execute(query) 
        print("Handed read connection!") 
        data =cur.fetchall() 
        json_data = json.dumps([dict(ix) for ix in data]) 
        print(json_data) 
        #con.commit() 
    except: 
        con.rollback() 
    finally: 
        a.putconn(con) yield json_data 

如何在上述函数中传递此data[0]['execution_time'] = query_execution_time

0 个答案:

没有答案