免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6185 | 回复: 9
打印 上一主题 下一主题

python使用mysqldb时查询的缓冲问题有没碰到过的? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-04-11 14:05 |只看该作者 |倒序浏览
我使用

            cur=conn.cursor()
            cur.execute(sql,para)
            data=cur.fetchall()
            cur.close()

当我在数据库手动删除一条记录时发现,返回的数据并没有变化,那么这里肯定是mysqldb存在缓冲,请问下这问题怎么处理?

论坛徽章:
0
2 [报告]
发表于 2009-04-11 17:17 |只看该作者
需要
conn.commit()

The connection object commit() method commits any outstanding changes in the current transaction to make them permanent in the database. In DB-API, connections begin with autocommit mode disabled, so you must call commit() before disconnecting or changes may be lost.

[ 本帖最后由 yaksavage 于 2009-4-11 17:22 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2009-04-13 09:03 |只看该作者
不好意思,楼上的理解错了。
我是说查询
比如a表有3条记录,我第一次通过python程序查询是三条,python不退出,我手动从数据库删除一条,再通过python查询,还是3条,这时应该是2条才对。

这里应该是有缓冲的,我不知道怎么来消除。避免不同进程对数据库操作的不统一。

论坛徽章:
0
4 [报告]
发表于 2009-04-13 09:29 |只看该作者
关注...

论坛徽章:
0
5 [报告]
发表于 2009-04-13 17:11 |只看该作者
貌似只能重新生成一个db connect实例
db实例里有set_server_option方法,试了禁止query_cache off,没效果,只能去源码看看。

论坛徽章:
0
6 [报告]
发表于 2009-04-14 17:54 |只看该作者
解决了,惯性影响,以前的query都不需要commit,而mysqldb不管怎样都需要做commit操作。

    def test_commit(self):
        con = self._connect()
        try:
            # Commit must work, even if it doesn't do anything
            con.commit()
        finally:
            con.close()

论坛徽章:
0
7 [报告]
发表于 2009-04-14 21:49 |只看该作者
发现不一样的结果,以下是我在OpenBSD下测试的结果:
Shell:~ >: python
Python 2.5.2 (r252:60911, Aug 12 2008, 11:14:20)
[GCC 3.3.5 (propolice)] on openbsd4
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>> db = MySQLdb.connect(host='127.0.0.1', db='test', user='Py3k', passwd='password')
>>> c = db.cursor()
>>> c.execute('select * from todo')
3L
>>> c.execute('select * from todo')
4L
>>> c.execute('select * from todo')
3L
>>> MySQLdb.__version__
'1.2.2'
>>>

论坛徽章:
0
8 [报告]
发表于 2009-04-17 08:31 |只看该作者

回复 #7 23号 的帖子

灵异时间,难道跟系统环境相关?
我这版本都是1.2.2的,除非按照6楼的commit,其他还是照旧。

论坛徽章:
0
9 [报告]
发表于 2009-04-17 09:26 |只看该作者
lz应该打开了自动事务,并且表用的innodb

论坛徽章:
0
10 [报告]
发表于 2014-11-20 12:53 |只看该作者
python MySQLdb 默认关闭了 autocommit,如果查询的是一个 innodb 表的话,一旦该表后面插入了新数据,之前的连接就会查不到新数据,比较蛋疼。
所以根据情况,一般情况下最好开启 autocommit

conn.autocommit(True)
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP