python连接mysql并提交mysql事务示例

代码如下:

# -*- 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()
”’

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2304376.html

(0)
上一篇 2025年2月28日 07:51:00
下一篇 2025年2月28日 07:51:22

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • 怎么通过Python实现linux远程登陆及sftp

    1. 使用shell命令 $ sshpass -p ${passwd} ssh -p ${port} -l ${user} -o StrictHostKeyChecking=no xx.xx.xx.xx “ls -l” 登录后复制 然后你会…

    编程技术 2025年3月30日
    100
  • Python怎么通过paramiko库实现远程执行linux命令

    (1)首先安装paramiko库 pip install paramiko 登录后复制 (2)封装了以下类,可以直接拿来使用 import paramikoclass SSHClient(object):    def __init__(s…

    编程技术 2025年3月30日
    100
  • python正则表达教程2024

    正则表达式是一种在文本中匹配模式的代码,使用元字符和量词定义匹配模式。Python 中使用 re 模块处理正则表达式:1. 导入 re 模块;2. 定义正则表达式模式;3. 调用 re.search() 查找匹配项。正则表达式语法包括元字符…

    2025年3月30日
    100
  • java中字符串怎么转数组

    在 Java 中,将字符串转换为数组的方法有:使用 split() 方法根据模式分隔字符串;使用 toCharArray() 方法获取单个字符的字符数组;使用 replaceAll() 和 StringJoiner 替换空格并连接非空格字符…

    2025年3月30日
    100
  • java怎么由字符串构造数组

    在 Java 中,可以通过以下六种方法将字符串构造为数组:使用 String.split() 方法拆分字符串使用正则表达式拆分字符串使用字符数组和 Arrays.copyOfRange() 方法使用流将字符串拆分为字符流使用 Apache …

    2025年3月30日
    100
  • java怎么将字符串给数组

    Java中将字符串转换为数组的方法有5种:使用toCharArray()方法、正则表达式、split()方法、Arrays.copyOf()方法和Stream API。具体选择取决于具体需求,不同方法有各自的优点和缺点。 如何在 Java …

    2025年3月30日
    100
  • notepad++版本介绍

    Notepad++ 是一款开源文本编辑器,自 2003 年起不断更新。目前最新版本是 10.0.0,具有语法高亮、多视图编辑、宏录制、正则表达式搜索、Markdown 预览、语法检查和代码重构等广泛功能。 Notepad++ 版本介绍 No…

    2025年3月30日
    100
  • notepad++的作用是什么

    Notepad++ 是一款免费的文本编辑器,主要用于编程、网页开发和数据分析等文本处理任务。其作用包括:语法高亮,识别不同语言代码结构和错误;代码自动完成,提高编码效率和准确性;支持自定义宏和插件,满足特定需求;多文档界面,实现多任务处理;…

    2025年3月30日
    100
  • 使用正则表达式匹配合法的 IPv4 地址的操作方法

    使用正则表达式匹配合法 IPv4 地址的方法:使用正则表达式 ^(([0-9]|1-9|1[0-9]{2}|20-4|25[0-5]).){3}([0-9]|1-9|1[0-9]{2}|20-4|25[0-5])$,匹配四段数字,每段 0-…

    2025年3月30日
    100
  • vscode是干嘛的 vscode的作用

    VS Code:不仅是代码编辑器,更是编程伙伴VS Code 是一款功能强大的集成开发环境(IDE),提供丰富的扩展和工具,显著提升编码效率:高度可扩展性:拥有庞大的扩展市场,覆盖几乎所有编程语言、框架和工具。核心功能:强大的内置调试器,支…

    2025年3月30日
    100

发表回复

登录后才能评论