代码如下:

# -*- coding: utf-8 -*-

import sys

import MySQLdb

reload(sys)

sys.setdefaultencoding('utf-8')

class DB(object):

def __init__(self,host='127.0.0.1',port=3306,user='root',passwd='123',database=''):

self.__host=host

self.__port=port

self.__user=user

self.__passwd=passwd

self.__database=database

self.__open=False

print '__init__'

def __connect__(self):

if self.__open == False:

print 'connect db...'

self.__conn = MySQLdb.connect(host=self.__host , port=self.__port , user=self.__user , passwd=self.__passwd,charset='utf8')

self.__open = True

def __executeSql__(self,sql):

self.__connect__()

self.__executor = self.__conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)

self.__executor.execute('use '+self.__database) #切换数据库

return self.__executor.execute(sql)

def executeQueryForObject(self , sql):

self.__executeSql__(sql)

return self.__executor.fetchone()

'''

返回key=value 字典

'''

def executeQueryAll(self , sql):

self.__executeSql__(sql)

return self.__executor.fetchall()

def executeUpdate(self ,sql='' , isAutoCommit=False):

c = self.__executeSql__(sql)

if isAutoCommit == True:

self.commit() #提交事务

return c

'''

#提交事务

'''

def commit(self):

self.__conn.commit() #提交事务

'''

#关闭数据库,释放资源

'''

def closeDB(self):

if not self.__conn is None:

print 'close db...'

self.__conn.commit() #提交事务

self.__conn.close()

def print_parameters(self):

print self.__user

print self.__passwd

print self.__host

print self.__port

'''

if __name__ == '__main__':

db=DB(database='tb2013')

#db.print_parameters()

#db.executeSql('select * from tb_user')

print db.executeQueryForObject('select count(*) as count from tb_user')

_rows = db.executeQueryAll('select userid,nick from tb_user limit 10');

print _rows

for row in _rows:

print row

print 'nick:%s' % str(row['nick'])

print db.executeUpdate(sql='update tb_user set nick=\'test\' where userid=95084397',isAutoCommit=True)

db.closeDB()

'''

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

python操作mysql事务提交_python连接mysql并提交mysql事务示例相关推荐

  1. python操作hive数据库代码_Python连接Hive操作数据库

    前言 客户端连接Hive需要使用HiveServer2.HiveServer2是HiveServer的重写版本,HiveServer不支持多个客户端的并发请求.当前HiveServer2是基于Thri ...

  2. python操作redis集群_python 连接管理作redis集群

    python的redis库是不支持集群操作的,推荐库:redis-py-cluster. 安装 pip3 install redis-py-cluster 连接redis集群 #!/usr/bin/e ...

  3. python连数据库课程设计_python 连接操作 各类数据库

    摘要: 用Python写脚本也有一段时间了,经常操作数据库(MySQL),现在就整理下对各类数据库的操作,如后面有新的参数会补进来,慢慢完善. 一,python 操作 MySQL:详情见:这里 mac ...

  4. mysql linux err2003_远程连接linux下的mysql Err1045 Err2003解决办法

    本人linux系统 Centos7 1.Err2003 我个人的情况是因为linux中防火墙开启并阻止了3306这个mysql端口的远程连接 解决办法: CentOS 7.0默认使用的是firewal ...

  5. python操作文件和目录_Python操作文件和目录

    Python操作文件和目录 读写文件比较简单,有一点特别注意就好了 windows下Python默认打开的文件以gbk解码,而一般我们的文件是utf-8编码的,所以如果文本含有中文,就会出现异常或者乱 ...

  6. mysql数据库被远程连接_如何让mysql数据库准许被远程连接访问

    第一:更改 "mysql" 数据库里的 "user" 表里的 "host" 项,从"localhost"改称'%'. 或 ...

  7. 访问他人mysql数据库_怎么连接访问他人的MYSQL数据库?

    1.访问别人的MYSQL方法: 找到MySQL配置文件,搜索"bind-address",找到这一行: bind-address=127.0.0.1在前面加一个#,注释掉这一行,保 ...

  8. Python结合mysql的实战_python连接mysql,实战。实现ATM类似功能。

    数据库,表建立. create database `mydata`; DROP TABLE IF EXISTS `mytab`; CREATE TABLE `mytab` ( `userid` int ...

  9. python如何修改excel数据库_python学习笔记-day7-2-【python从mysql数据库导数据到excel,读excel,修改excel】...

    这节说下如何用python把数据库里的数据导出到excel里,并如何读取excel, 修必excel等操作. 一.用python把数据库里的数据导出到excel里 1.导入如下的模块,没有的话需要安装 ...

最新文章

  1. 互换性与技术测量电子版_181套建设工程全套资料表格,从开工到完工,完整电子版手慢无...
  2. 华为云微认证答题_158人次通过华为认证考试!今年,湖北这所高校频现大动作...
  3. Puppet 之 模板和模块
  4. 互联网的未来之上:平权的互联网
  5. git使用教程(初次配置+错误提示)
  6. s3c2440的GPIO驱动
  7. TCP/IP协议与Http协议的区别
  8. 在线思维导图工具-toolfk程序员在线工具网
  9. 联想小新13pro锐龙版网卡_4499元诠释极致性价比 联想小新Pro 13标压锐龙版上手...
  10. 【转载】机器指令计算机
  11. U盘制作成启动盘后容量变小
  12. 华住2.0? 5亿万豪用户信息泄漏! 个人隐私如何保护? 离不开区块链
  13. 智能快递柜在小区的应用
  14. ancestral 箭头符号,英文字典
  15. win7安装eclipse
  16. Android Verified Boot dm-verity 优化和实战
  17. windows查看系统信息的方法
  18. Java类和对象知识点的整理
  19. [运放滤波器]5_滤波器的分类
  20. 64位 计算机 最大内存,请问win7系统 最大识别内存多少,支持最大硬盘空间多少?...

热门文章

  1. 内容创业时代,粉丝已死
  2. 华为荣耀手机指令代码大全_双十二,华为/荣耀手机推荐选购指南,全系列横评推荐,那一款华为/荣耀手机最值得够买...
  3. Python 常见优化技巧,让你的程序溜溜的跑起来!
  4. mysql 手机号 字段_2021-01-06:mysql中,我存十亿个手机号码,考虑存储空间和查询效率,用什么类型的字段去存?...
  5. hana修改字段_自定义SAP HANA登陆界面背景
  6. C语言编译报错:incompatible pointer type [-Wincompatible-pointer-types](传参类型不匹配)
  7. 【记忆断层、记忆裂痕】
  8. IDLE 放置奇兵 算法 地牢 记录
  9. 测试socket udp 单次传输数据上限(sendto()函数data不能超过65507字节。udp头占8字节,ip头占20字节,加起来正好65535字节)
  10. 如何查看windows版本号?