class DBBaseHandle: def __init__(self): try: self.conn = MySQLdb.connect(host=syn_config.mysql_server.get('host'), user=syn_config.mysql_server.get('user'), passwd=syn_config.mysql_server.get('passwd'), db=syn_config.mysql_server.get('db'),charset=syn_config.mysql_server.get('charset')) self.cursor = self.conn.cursor() except Exception,e: logger.error('connect mysql error:%s'%e) self.conn = MySQLdb.connect(host=syn_config.mysql_server.get('host'), user=syn_config.mysql_server.get('user'), passwd=syn_config.mysql_server.get('passwd'), db=syn_config.mysql_server.get('db'),charset=syn_config.mysql_server.get('charset')) self.cursor = self.conn.cursor() def close(self): self.conn.close() self.cursor.close() def rollback(self): self.conn.rollback() class DBHandle(DBBaseHandle): def get_all_follow(self,uid): try: data = [] self.cursor.execute("select uid2 from user_following where uid=%s;"%uid) result = self.cursor.fetchall() if result: for res in result: data.append(res[0]) self.close() return data finally: ###添加了此处解决问题 self.conn.commit() ###添加了此处解决问题 class TestHandle(DBBaseHandle): def test(self,uid,vuids): try: for vuid in vuids: self.cursor.execute("insert into user_following(uid,uid2,follow_time) values(%s,%s,%s);"%(int(uid),int(vuid),int(time.time()))) return True #except : 唯一性错误 except Exception,e: logger.error('uid:%s,create follow error:%s'%(uid,e)) print e self.rollback() return False finally: self.conn.commit() if __name__ == "__main__": logger.info("start test ******************") d1 = DBHandle.get_all_follow(123) TestHandle().test(123,[1,2,3,4]) d2 = DBHandle.get_all_follow(123)
当代码中没有红色部分时,执行代码,d1与d2是一样的,这儿就是mysqldb存在缓冲的问题
解决方法:
1、语句末尾加上“COMMIT;”
2 、运行完语句,至少在关闭数据库之前提交一下,如:conn.commit()
3、数据库连接建立之后,设置自动提交,如:conn.autocommit(1)
mysql不同的存储引擎 对应的commit不同,所以要针对不同的存储引擎打开autocommit或关闭。MySQLdb 在连接后关闭了自动提交,自动提交对于 innodb 引擎很重要,没有这个设置,innodb 引擎就不会真正执行语句。
参考:http://www.cnblogs.com/coser/archive/2012/01/12/2320741.html
相关推荐
这个包里有两个包,都可以用来在windows上安装python-MySQLdb,选择其中一种就可以!我比较喜欢用-master那个包,因为只要解压后执行python setup.py install就可以了。另一个包在安装时若出现找不到python2.7的安装...
python安装模块MySQLdb时,提示python2.7未安装,安装失败时,命令行python register.py,然后重新安装MySQLdb,就能正确安装。
windows环境下配置python3 mysqldb
python2.6-MySQLdb,包括python2.6安装包、MySQLdb for python2.6安装包、MySQLdb使用配置,为想学习Python操作mysql数据库的同学提供了极大的方便。。。
python下MySQLdb使用.docx
python下MySQLdb使用.pdf
python2.7.5安装mysqldb失败, my_config.h: No such file or directory. 解决办法:1)mysql版本太高,可降低版本 --此路一般不会考虑 2)注意下载的mysql-python的版本是否符和当前版本兼容 2)下载附件中的文件,...
python3.6使用MySQLdb连接mysql需要mysqlclient-1.3.12-cp36-cp36m-win_amd64.whl
这里要理解一个概念,python操作数据库,都是需要一个类似MySQLdb这样的中间层,这些中间层抽象了具体的实现,提供了统一的API供开发者使用。 二、 如何安装MySQLdb? python2环境下: sudo pip install MySQL-...
鸟人封装的MySQLdb操作接口,包含读写分离、断线重连的逻辑,对于具体应用可以继承和扩展
python下MySQLdb使用[归纳].pdf
Python-MySQLdb-examplePython-MySQLdb-example本文主要介绍通过MySQLdb实现Python对MySQL数据库的访问和操作。备注:execute 把一条查询语句发送给mysql服务器,这里有两种情况:CREATE、INSERT、DELETE、UPDATE、...
2021_w_.1.python 驱动MySQLdb(create_engine)代码
MySQL-python-1.2.5.win-amd64-py2.7
连接MySQLdb1-MySQLdb-1.2.5.zip MySQLdb1-MySQLdb-1.2.5.zip MySQLdb1-MySQLdb-1.2.5.zip MySQLdb1-MySQLdb-1.2.5.zip
windows安装python mysqldb时报错python version 2.7 required,which was not found in the registry 网上很多方案,比如方案一: Python3.x时, from _winreg import * 改为 from winreg import * 去掉下划线 ...
课题-Python-MySQLdb-学习总结.docx
关于python的MySQldb模块的入库应用实例,将某文件夹下的文件解析后入库并删除原文件。