我试图在表格中检索行数 与
<span class="image">
<img src="http://via.placeholder.com/100x100">
<p>some text</p>
</span>
<span>
<img class="image" src="http://via.placeholder.com/100x100">
<p>a really really really really really really really really really really long bit of text</p>
</span>
<span>
<img class="image" src="http://via.placeholder.com/100x100">
<p>some text</p>
</span>
,结果是
import postgresql
db = postgresql.open(...)
res = db.query("select count(1) from testdata")
print (res)
如何打印 10 ?
提前致谢
答案 0 :(得分:1)
db.query("select count(1) from testdata")
返回一个元组,而不是单个值。这个元组的第一个参数是查询的结果:
import postgresql
db = postgresql.open(...)
res = db.query("select count(1) from testdata")
count_result = res.next()
print (count_result)
这将使用查询响应的next
方法读取查询结果,以获取第一个结果。由于我们使用了count
,因此我们应该只有一个带有计数的响应(参见Data Wrangling with Python p.212)。
正如评论所说,有其他方法可以实现这一目标:
import postgresql
db = postgresql.open(...)
res = db.query("select count(1) from testdata")
print (res[0]) #first argument of res is the count
import postgresql
db = postgresql.open(...)
res, = db.query("select count(1) from testdata") # first argument assigned to res
# all subsequent arguments unassigned
print (res)