python语言中,对于cursor的查询明明有结果,为什么print cursor.rowcount结果为-1?

来源:互联网 发布:dvi线和vga线的端口 编辑:程序博客网 时间:2024/05/22 15:03

从网上搜了很多资料,没有对口的,抓狂。没办法自己研究了一下,相信写出来能够帮到很多人。

 

下边是我写的一段代码,为了方便大家看我加了行号。从下边代码中第4行可以看到,rowcount结果为-1,但是从第5行却可以将查询结果打印出来。显然查询是有结果的,为什么rowcount为-1呢?

第1行>>> db = mysql.connector.Connect(host=‘10.1.1.47’, port=2205, user=‘boss’, passwd=‘qwekkk’,charset=utf8,db='PP_01')第2行>>> cursor_pet = db.cursor()第3行>>> cursor_pet.execute('select userid,petid from t_pp_pet_01')第4行>>> print cursor_pet.rowcount-1第5行>>> for list in cursor_pet.fetchall():...     print list... (1, 11)(1, 12)(2, 11)(3, 11)(2, 10)(4, 15)


下边是解决方案。我们应该在打印查询行数之前先调用一下fetchall()这个函数,然后行数rowcount就打印出来啦:

第1行>>>db = mysql.connector.Connect(host=‘10.1.1.47’, port=2205, user=‘boss’, passwd=‘qwekkk’,charset=utf8,db='PP_01')第2行>>> cursor_pet = db.cursor()第3行>>> cursor_pet = db.cursor()第4行>>> cursor_pet.execute('select userid,petid from t_pp_pet_01')第5行>>> cursor_pet.fetchall()[(1, 11), (1, 12), (2, 11), (3, 11), (2, 10), (4, 15)]第6行>>> print cursor_pet.rowcount6


 



 


 

原创粉丝点击