获取此查询返回的行数的正确方法是什么?我特别想看看是否没有返回结果。
sql = 'SELECT count(*) from table WHERE guid = %s;'
data=[guid]
cur.execute(sql,data)
results = cur.fetchone()
for r in results:
print type(r) # Returns as string {'count': 0L} Or {'count': 1L}
获取此查询返回的行数的正确方法是什么?我特别想看看是否没有返回结果。
sql = 'SELECT count(*) from table WHERE guid = %s;'
data=[guid]
cur.execute(sql,data)
results = cur.fetchone()
for r in results:
print type(r) # Returns as string {'count': 0L} Or {'count': 1L}
3条答案
按热度按时间ct2axkht1#
results
本身是一个行对象,在您的例子中(根据print
输出判断),是一个字典(您可能配置了一个类似于dict的游标子类);只需访问count
键:因为使用了
.fetchone()
,所以只返回一行,而不是一个行列表。如果你 not 使用dict(-like)行游标,行是元组,count值是第一个值:
erhoui1w2#
这可能对那些遇到这个线程的人有帮助,这里是一种使用Python计算数据库中每个表的所有行的方法:
dzhpxtsq3#
以下为我工作
**缺点:如果你需要的只是计数,那么这对DBMS来说不是很有效。