This topic created in 4783 days ago, the information mentioned may be changed or developed.
def query_db(query,args = (),one=False):
"""QUeries the database and returns a list of dictionaries."""
cur = get_db().execute(query,args)
rv = cur.fetchall()
return (rv[0] if rv else None) if one else rv
最后一句return 的应该怎么理解?
6 replies • 1970-01-01 08:00:00 +08:00
 |
|
1
liluo May 3, 2013
if one: if rv: return rv[0] else: return None else: return rv
|
 |
|
2
liluo May 3, 2013
if one: ....if rv: ........return rv[0] ....else: ........return None else: ....return rv
代码缩进不能,请将 . 脑补成空格.
|
 |
|
3
dreampuf May 3, 2013
三元操作符的组合判断。
Sqlite API, Django ORM 或者 SQLAlchemy 都支持获取一个的操作,建议使用原生API e.g. `cur.fetchone()`
|
 |
|
4
C0VN May 3, 2013
中间的那一行是这个吧: rv = [dict((cur.description[idx][0].lower(), value) for idx, value in enumerate(row)) for row in cur.fetchall()]
lz看明白这一行没?!
(True: do...) if xxx else (False: do...)
|
 |
|
5
hhrmatata May 3, 2013
没有代码缩进好难看啊 不知道v2ex为什么不支持。
|