开发者

Python Flask - Easy Querying Function

Can somebody explain me the last line return (rv[0] if rv else None) if one else rv ? Especially the role of one.

def query_db(query, args=(), one=False):
    cur = g.db.execute(query, args)
    rv = [dict((cur.description[idx][0], value)
               for idx, value in enumerate(row)) 开发者_如何转开发for row in cur.fetchall()]
    return (rv[0] if rv else None) if one else rv


one indicates whether or not to return only a single record. If one is true then it returns the first (rv[0]) if there are records to be found (if rv), otherwise it returns all the records.


By default, one is False, so by default the list-of-dicts generated from the fetchall gets returned.

If you pass True for one, you get only the first row (turned into a dict) of the query, or None if the query didn't return any rows.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜