Chinaunix

标题: python使用mysqldb时查询的缓冲问题有没碰到过的? [打印本页]

作者: 都是虫子惹的祸    时间: 2009-04-11 14:05
标题: python使用mysqldb时查询的缓冲问题有没碰到过的?
我使用

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

当我在数据库手动删除一条记录时发现,返回的数据并没有变化,那么这里肯定是mysqldb存在缓冲,请问下这问题怎么处理?
作者: yaksavage    时间: 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 编辑 ]
作者: 都是虫子惹的祸    时间: 2009-04-13 09:03
不好意思,楼上的理解错了。
我是说查询
比如a表有3条记录,我第一次通过python程序查询是三条,python不退出,我手动从数据库删除一条,再通过python查询,还是3条,这时应该是2条才对。

这里应该是有缓冲的,我不知道怎么来消除。避免不同进程对数据库操作的不统一。
作者: 23号    时间: 2009-04-13 09:29
关注...
作者: annyraul    时间: 2009-04-13 17:11
貌似只能重新生成一个db connect实例
db实例里有set_server_option方法,试了禁止query_cache off,没效果,只能去源码看看。
作者: 都是虫子惹的祸    时间: 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()
作者: 23号    时间: 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'
>>>
作者: xiaoyu9805119    时间: 2009-04-17 08:31
标题: 回复 #7 23号 的帖子
灵异时间,难道跟系统环境相关?
我这版本都是1.2.2的,除非按照6楼的commit,其他还是照旧。
作者: 3227049    时间: 2009-04-17 09:26
lz应该打开了自动事务,并且表用的innodb
作者: pader_it168    时间: 2014-11-20 12:53
python MySQLdb 默认关闭了 autocommit,如果查询的是一个 innodb 表的话,一旦该表后面插入了新数据,之前的连接就会查不到新数据,比较蛋疼。
所以根据情况,一般情况下最好开启 autocommit

conn.autocommit(True)




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2