python web py入门(11)- 双重查询访问MYSQL数据库出错

像下面这段代码,就会导致嵌套访问数据连接,因而python的mysql连接器就会抛出错误,代码如下:

 posts = db.query('''SELECT posts.id, title, posts.time, user_id, users.name AS username
                            FROM posts JOIN users
                            ON posts.user_id = users.id
                            ORDER BY posts.id DESC
                            LIMIT %d OFFSET %d''' % (per_page, offset))
        page_posts = []
        for p in posts:
            comment = Comment(p.id)
            comment.count()
            last = comment.last()
            last_time = last.time if last else p.time
            page_posts.append({'id': p.id, 'title': p.title, 'userid': p.user_id, 'username': p.username, 'comment_count': comment.count(), 'last_time': last_time})
在这里第一个查询 posts = db.query,接着遍历数据结果集时,又来了一个查询comment.count(),这时就会抛出下面的错误:
Traceback (most recent call last):
  File "D:\AI\python\web\caiforum\forumTest.py", line 40, in <module>
    page_posts, page_count = model.Post().list(page)
  File "D:\AI\python\web\caiforum\model.py", line 35, in list
    page_posts.append({'id': p.id, 'title': p.title, 'userid': p.user_id, 'username': p.username, 'comment_count': comment.count(), 'last_time': last_time})
  File "D:\AI\python\web\caiforum\model.py", line 172, in count
    result = db.query("SELECT COUNT(*) AS count FROM comments WHERE parent_id=%d" % self.__parent_id)
  File "C:\Users\tony\AppData\Local\Programs\Python\Python36\lib\site-packages\web.py-0.40.dev0-py3.6.egg\web\db.py", line 664, in query
    db_cursor = self._db_cursor()
  File "C:\Users\tony\AppData\Local\Programs\Python\Python36\lib\site-packages\web.py-0.40.dev0-py3.6.egg\web\db.py", line 575, in _db_cursor
    return self.ctx.db.cursor()
  File "C:\Users\tony\AppData\Local\Programs\Python\Python36\lib\site-packages\mysql\connector\connection.py", line 845, in cursor
    self.handle_unread_result()
  File "C:\Users\tony\AppData\Local\Programs\Python\Python36\lib\site-packages\mysql\connector\connection.py", line 1109, in handle_unread_result
    raise errors.InternalError("Unread result found")
mysql.connector.errors.InternalError: Unread result found


原因如下:
The reason is that without a buffered cursor, the results are "lazily" loaded, meaning that "fetchone" actually only fetches one row from the full result set of the query. When you will use the same cursor again, it will complain that you still have n-1 results (where n is the result set amount) waiting to be fetched. However when you use a buffered cursor the connector fetches ALL rows behind the scenes and you just take one from the connector so the mysql db won't complain. Hope it helps.


大体意思就是mysql连接游标只会获取一行结果返回,如果在迭代过程中,再去使用相同的游标,就会出错。知道原因解决起来,就简单了,就是使用缓存游标,或者使用双连接来解决。

cursor = cnx.cursor(buffered=True)

或者:

config={'host':'127.0.0.1',#default localhost  
        'user':'root',  
        'password':'buaascse',  
        'port':3306 ,#默认即为3306  
        #'database':'mobilephone', 无默认数据库  
        'charset':'utf8',#默认即为utf8  
        'buffered': True,  
        }  
    try:  
        cnn = mysql.connector.connect(**config)  
    except mysql.connector.Error as e:  
         print('connect fails!{}'.format(e))  
         

在VC2015里学会使用tinyxml库
http://edu.csdn.net/course/detail/2590
在Windows下SVN的版本管理与实战 
http://edu.csdn.net/course/detail/2579
Visual Studio 2015开发C++程序的基本使用 
http://edu.csdn.net/course/detail/2570
在VC2015里使用protobuf协议
http://edu.csdn.net/course/detail/2582
在VC2015里学会使用MySQL数据库
http://edu.csdn.net/course/detail/2672



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

caimouse

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值