首页 > 数据库技术 > 详细

Python数据库工具类MySQLdb使用

时间:2015-03-26 19:30:58      阅读:341      评论:0      收藏:0      [点我收藏+]
MySQLdb模块用于连接mysql数据库。
基本操作
# -*- coding: utf-8 -*-      
#
mysqldb      
import time, MySQLdb      
     
#连接      
conn=MySQLdb.connect(host="localhost",user="root",passwd="root",db="test",charset="utf8")    
cursor = conn.cursor()      
 
#删除表  
sql = "drop table if exists user"  
cursor.execute(sql)  
 
#创建  
sql = "create table if not exists user(name varchar(128) primary key, created int(10))"  
cursor.execute(sql)  
 
#写入      
sql = "insert into user(name,created) values(%s,%s)"    
param = ("aaa",int(time.time()))      
n = cursor.execute(sql,param)      
print insert,n      
     
#写入多行      
sql = "insert into user(name,created) values(%s,%s)"    
param = (("bbb",int(time.time())), ("ccc",33), ("ddd",44) )  
n = cursor.executemany(sql,param)      
print insertmany,n      
 
#更新      
sql = "update user set name=%s where name=‘aaa‘"    
param = ("zzz")      
n = cursor.execute(sql,param)      
print update,n      
     
#查询      
n = cursor.execute("select * from user")      
for row in cursor.fetchall():      
    print row  
    for r in row:      
        print r      
     
#删除      
sql = "delete from user where name=%s"    
param =("bbb")      
n = cursor.execute(sql,param)      
print delete,n      
 
#查询      
n = cursor.execute("select * from user")      
print cursor.fetchall()      
 
cursor.close()      
     
#提交      
conn.commit()  
#关闭 
conn.close()
     

      

 

封装类操作


 class DButils(object):
    def __init__(self,filename,section):
        super(DButils, self).__init__()
        #read config
        cfg = ConfigUtils(filename).config
        self.cfg = cfg
        self.section = section
        #init mysql connection
        self.conn= MySQLdb.connect(
            host=cfg.get(section,host),
            port = cfg.getint(section,port),
            user=cfg.get(section,user),
            passwd=cfg.get(section,passwd),
            db=cfg.get(section,db),
            connect_timeout=cfg.getint(section,connect_timeout)
        )
        self.cur = self.conn.cursor()    
     
    def fetchmany(self,sql):
        sql = sql.replace({$db},self.cfg.get(self.section,db))
        try:
            return self.cur.fetchmany(self.cur.execute(sql))
        except Exception, e:
            print traceback.print_exc()
            print sql
   
    def fetchone(self,sql):
        sql = sql.replace({$db},self.cfg.get(self.section,db))
        try:
            self.cur.execute(sql)
            return self.cur.fetchone()
        except Exception, e:
            print traceback.print_exc()
            print sql
    def create(self,sql):
        try:
            self.cur.execute(sql)
            self.conn.commit()
        except Exception, e:
            print traceback.print_exc()
    def select_limit(self):
        return self.cfg.get(src_mysql,limit)
    def is_table_exit(self,tableName):
        show_sql = show tables;
        try:
            return tableName in self.cur.fetchmany(self.cur.execute(show_sql))
        except Exception,e:
            print traceback.print_exc()
    def close_db(self):
        self.cur.close()
        self.conn.close()

db    = DButils(ini.cfg,src_db)
try:
    db.fetchone(‘‘)
finally:
    db.close_db()

 

Python数据库工具类MySQLdb使用

原文:http://www.cnblogs.com/snifferhu/p/4369184.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!