码迷,mamicode.com
首页 > 数据库 > 详细

MySQL操作工具包

时间:2019-12-17 00:34:29      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:__init__   执行sql   事务   fail   http   参数   into   update   tab   

#-*- encoding: utf-8 -*-
‘‘‘
@describe: 读取mysql数据库的工具类
‘‘‘
import sys
sys.path.append("/home/hadoop/crawler")
from configs import config
import MySQLdb

class DBUtil:
    
    def __init__(self, db):
        self.db = MySQLdb.connect(host=db[HOST], user=db[USER], passwd=db[PASSWD], db=db[DB], charset=db[CHARSET], port=db[PORT])
        
    def read_one(self,sql, params = None):
        ‘‘‘
        select a,b,c from table
        :return  (a,b,c)
        ‘‘‘
        self.cursor = self.db.cursor()
        if params == None:
            self.cursor.execute(sql)
        else:
            self.cursor.execute(sql, params)
        return self.cursor.fetchone()
        
    def read_tuple(self, sql, params = None):
        """execute sql return tuple
        select a,b,c from table
        ((a,b,c),(a,b,c))
        """
        self.cursor = self.db.cursor()
        if params == None:
            self.cursor.execute(sql)
        else:
            self.cursor.execute(sql, params)
        return self.cursor.fetchall()
    
    def read_dict(self, sql, params = None):
        """execute sql return dict
        select a,b,c from table
        ({a:1,b:2,c:33},{a:1,b:3,c:45})
        """
        self.cursor = self.db.cursor(cursorclass=MySQLdb.cursors.DictCursor)
        if params == None:
            self.cursor.execute(sql)
        else:
            self.cursor.execute(sql, params)
        return self.cursor.fetchall()
    
    def executemany(self,sql, params):
        ‘‘‘
        insert into table (a,b,c) values(?,?,?)
        values   [(1,2,3),(324,6,1),(11,5,5)]
        :return:
        ‘‘‘
        self.cursor = self.db.cursor()
        self.cursor.executemany(sql, params)
        self.db.commit()

    def executemany_no_commit(self,sql, params):
        self.cursor = self.db.cursor()
        self.cursor.executemany(sql, params)


    def execute(self,sql,params = None):
        ‘‘‘
        执行SQL语句自动提交,防止SQL注入
        :param sql: SQL
        :param params: 参数
        :return:
        ‘‘‘
        self.cursor = self.db.cursor()
        if params == None:
            self.cursor.execute(sql)
        else:
            self.cursor.execute(sql, params)
        self.db.commit()

    def execute_no_commit(self,sql, params = None):
        ‘‘‘
        执行SQL语句不自动提交,防止SQL注入
        :param sql: SQL
        :param params: 参数
        :return:
        ‘‘‘
        self.cursor = self.db.cursor()
        if params == None:
            self.cursor.execute(sql)
        else:
            self.cursor.execute(sql, params)

    def commit(self):
        self.db.commit()

    def close(self):
        """close db connect
        """
        self.cursor = self.db.cursor()
        self.cursor.close()
        self.db.close()
    
    def rollback(self):
        """rollback db connect
        """
        self.db.rollback()
        
    def rollback_close(self):
        """rollback and close db connect
        """
        self.db.rollback()
        self.db.close()        
    
if __name__ == __main__:
    db = DBUtil(config._HAINIU_DB)

    #初始化数据
    # sql = """
    # insert into hainiu_queue
    #     (type,action,params,fail_ip,create_times)
    #     values (%s,%s,%s,%s,%s);
    # """
    #
    # for i in range(0,20):
    #     params = [1,"http://mil.huanqiu.com/?agt=15438","Baltimore Ravens","Sports Team","2015-08-04 21:35:40"]
    #     db.execute(sql, params)

    # 事务测试
    # dataT = db.read_dict("select id,action,params from hainiu_queue where type=1 limit 0,1 for update;")
    # for objs in dataT:
    #     print objs
    #
    #
    # id = dataT[0]["id"]
    #
    # print id
    # sql = "update hainiu_queue set type=0 where id=%s"
    # db.execute_no_commit(sql,id)
    #
    # db.commit()
    #
    # db.close()
    

MySQL操作工具包

标签:__init__   执行sql   事务   fail   http   参数   into   update   tab   

原文地址:https://www.cnblogs.com/yoyowin/p/12052134.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!