Mat*_*att 15 python postgresql psycopg2
什么是获取此查询返回的数字或行的正确方法?我特意想看看是否没有返回任何结果.
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}
Run Code Online (Sandbox Code Playgroud)
谢谢.
Mar*_*ers 27
results
在你的情况下(通过声明的print
输出判断),一个字体(你可能配置了类似dict的游标子类)本身就是一个行对象; 只需访问count
密钥:
result = cur.fetchone()
print result['count']
Run Code Online (Sandbox Code Playgroud)
因为您.fetchone()
只使用了一行返回,而不是行列表.
如果您没有使用dict(类似)行游标,则行是元组,计数值是第一个值:
result = cur.fetchone()
print result[0]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
20614 次 |
最近记录: |