python操作mysql数据库—坑吭

一、原始代码

#coding=utf-8

import MySQLdb
from cProfile import Profile


class MySQLUtils():
    def __init__(self):
        self.connect = MySQLdb.connect(host="192.168.111.1", port=3306, user="root", passwd="root", db="test", charset='utf8')
        self.cursor = self.connect.cursor()
        self.connect.autocommit(on=True)

    def finish(self):
        self.connect.close()

    def execute(self,sql,values):
        self.cursor.execute(sql,values)

    def fetch_one(self):
        return self.cursor.fetchone()


    def fetch_all(self):
        return self.cursor.fetchall()


def mysql_execute(sql,values):
    dbutils = MySQLUtils()
    dbutils.execute(sql,values)
    all_result = dbutils.fetch_all()
    dbutils.finish()
    return all_result

sql = 'insert into scan_domain(top_level_domain,domain) values(%s,%s)'

def execute_test():
    for i in range(0, 100000):
        mysql_execute(sql, ['aaa.com', 'hao.aaa.com'])

prof = Profile()
prof.enable()
execute_test()
prof.create_stats()
prof.print_stats()

 

二、改进一版,使用数据库连接池

#coding=utf-8

import MySQLdb
from DBUtils.PooledDB import PooledDB
from cProfile import Profile


class MySQLUtils():

    __pool = None

    def __init__(self):
        self.connect = MySQLUtils.__get_connect()
        self.cursor = self.connect.cursor()

    @staticmethod
    def __get_connect():
        if MySQLUtils.__pool is None:
            MySQLUtils.__pool = PooledDB(creator=MySQLdb, mincached=1, maxcached=20,
                              host='127.0.0.1', port=3306, user='root', passwd='root',
                              db='test', use_unicode=False, charset='utf8')
        return MySQLUtils.__pool.connection()

    def finish(self):
        self.cursor.close()
        self.connect.close()
        MySQLUtils.__pool.close()

    def execute(self,sql,values=[]):
        count = self.cursor.execute(sql,values)
        self.connect.commit()
        if count:
            return list(self.fetch_all())

    def fetch_one(self):
        return self.cursor.fetchone()

    def fetch_all(self):
        return self.cursor.fetchall()



prof = Profile()
sql = 'insert into scan_domain(top_level_domain,domain) values(%s,%s)'
prof.enable()
dbutils = MySQLUtils()

def execute_test():
    for i in range(0,100000):
        result = dbutils.execute(sql=sql, values=['aaa.com', 'hao.aaa.com'])
execute_test()
dbutils.finish()
prof.create_stats()
prof.print_stats()

 

 

三、细节

#coding=utf-8

import MySQLdb
from DBUtils.PooledDB import PooledDB
from cProfile import Profile


class MySQLUtils():

    __pool = None

    def __init__(self):
        self.connect = MySQLUtils.__get_connect()
        self.cursor = self.connect.cursor()

    @staticmethod
    def __get_connect():
        if MySQLUtils.__pool is None:
            MySQLUtils.__pool = PooledDB(creator=MySQLdb, mincached=1, maxcached=20,
                              host='127.0.0.1', port=3306, user='root', passwd='root',
                              db='test', use_unicode=False, charset='utf8')
        return MySQLUtils.__pool.connection()

    def finish(self):
        self.connect.commit()
        self.cursor.close()
        self.connect.close()
        MySQLUtils.__pool.close()

    def execute(self,sql,values=[]):
        count = self.cursor.execute(sql,values)
        # self.connect.commit()
        if count:
            return list(self.fetch_all())

    def fetch_one(self):
        return self.cursor.fetchone()

    def fetch_all(self):
        return self.cursor.fetchall()



prof = Profile()
sql = 'insert into scan_domain(top_level_domain,domain) values(%s,%s)'
prof.enable()
dbutils = MySQLUtils()

def execute_test():
    for i in range(0,100000):
        result = dbutils.execute(sql=sql, values=['aaa.com', 'hao.aaa.com'])
execute_test()
dbutils.finish()
prof.create_stats()
prof.print_stats()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值