博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python库之MySQLdb介绍
阅读量:4046 次
发布时间:2019-05-24

本文共 2511 字,大约阅读时间需要 8 分钟。

今天想用python给公司写个小的程序,实现从mysql中读取数据,update等操作,结果发现目前python竟然没有官方库,搜了一下,找到个应用比较多的,今天的主角–“MySQLdb”
网址:http://mysql-python.sourceforge.net/

使用手册:http://mysql-python.sourceforge.net/MySQLdb.html

我的博客:

其实就是对mysql的 C API进行了封装,支持事务(5.0以上,且用InnoDB方式)
使用步骤:
1.安装python
2.根据python的版本下载MySQLdb的安装包(也可以自己编译),注意一定要对应python的版本,3.2的安装包是MySQL-python-1.2.3.win32-py3.2.exe,自己可以对应搜索下,有一个比较全的网站,但是我找不到了

3.写应用,下面是我封装的一个mysql操作类,支持事务,代码请参考,时间关系我并没有加注释,不过应该很好懂,你也可以根据自己需求封装一下

#===============================================================================# -*- coding: utf-8 -*-#MySQLdb封装类#author:paul wang#===============================================================================import MySQLdb as mdbclass myMySQL:    def connect(self,host="localhost",user="root",pwd="",database="",autocommit=False):        try:            self.isConnect = False            self.conn = mdb.connect( host, user,                pwd, database);            self.isConnect = True            self.cursor = self.conn.cursor()            self.cursor.execute("SELECT VERSION()")            data = self.cursor.fetchone()            if autocommit:                self.conn.autocommit(True)            else:                self.conn.autocommit(False)        except mdb.Error as e:            print ( "Connect Error %d: %s" % (e.args[0],e.args[1]) )        print ( "Database version : %s " % data )    def close(self):        try:            self.cursor.close()            self.conn.close()        except mdb.Error as e:            print ( "Close Error %d: %s" % (e.args[0],e.args[1]) )    def excute(self,sql=""):        try:            self.cursor.execute(sql)        except mdb.Error as e:            print ( "Excute Error %d: %s" % (e.args[0],e.args[1]) )            print ( "Excute sql= %s" % sql )    def getrows(self,sql):        try:            self.excute(sql)            rows = self.cursor.fetchall()            return rows        except mdb.Error as e:            print ( "getrows Error %d: %s" % (e.args[0],e.args[1]) )       def selectDB(self,dbName):        self.conn.select_db(dbName)    def commit(self):        self.conn.commit()    def rollback(self):        self.conn.rollback()    def setautocommit(self,auto=False):        self.conn.autocommit(auto)    def isConnected(self):        return self.isConnect#下面是测试代码#db = myMySQL()#db.connect( "localhost","root","","drupal",False )#db.setautocommit(False)#db.excute("insert into test values(2)")#db.rollback()#rows = db.getrows("select * from test where 1 = 1 ")#print( rows )#db.close()

转载地址:http://rggdi.baihongyu.com/

你可能感兴趣的文章
Java.nio
查看>>
函数模版类模版和偏特化泛化的总结
查看>>
VMware Workstation Pro虚拟机不可用解决方法
查看>>
最简单的使用redis自带程序实现c程序远程访问redis服务
查看>>
redis学习总结-- 内部数据 字符串 链表 字典 跳跃表
查看>>
iOS 对象序列化与反序列化
查看>>
iOS 序列化与反序列化(runtime) 01
查看>>
iOS AFN 3.0版本前后区别 01
查看>>
iOS ASI和AFN有什么区别
查看>>
iOS QQ侧滑菜单(高仿)
查看>>
iOS 扫一扫功能开发
查看>>
iOS app之间的跳转以及传参数
查看>>
iOS __block和__weak的区别
查看>>
Android(三)数据存储之XML解析技术
查看>>
Spring JTA应用之JOTM配置
查看>>
spring JdbcTemplate 的若干问题
查看>>
Servlet和JSP的线程安全问题
查看>>
GBK编码下jQuery Ajax中文乱码终极暴力解决方案
查看>>
Oracle 物化视图
查看>>
PHP那点小事--三元运算符
查看>>