一、原始代码

#coding=utf-8import MySQLdb
from cProfile import Profileclass 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_resultsql = '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-8import MySQLdb
from DBUtils.PooledDB import PooledDB
from cProfile import Profileclass MySQLUtils():__pool = Nonedef __init__(self):self.connect = MySQLUtils.__get_connect()self.cursor = self.connect.cursor()@staticmethoddef __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-8import MySQLdb
from DBUtils.PooledDB import PooledDB
from cProfile import Profileclass MySQLUtils():__pool = Nonedef __init__(self):self.connect = MySQLUtils.__get_connect()self.cursor = self.connect.cursor()@staticmethoddef __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()

python操作mysql数据库—坑吭相关推荐

  1. python操作mysql数据库的常用方法使用详解

    python操作mysql数据库 1.环境准备: Linux 安装mysql: apt-get install mysql-server 安装python-mysql模块:apt-get instal ...

  2. Python操作mySql数据库封装类

    这是自己在做项目过程中,整理封装的操作mysql数据库封装类,自己可以修改下使用,节省大量时间. myGlobal.py # -*- coding: utf-8 -*-global globalLis ...

  3. python操作mysql数据库(增、删、改、查)_python对 MySQL 数据库进行增删改查的脚本...

    # -*- coding: utf-8 -*- import pymysql import xlrd # import codecs #连接数据库 conn = pymysql.connect(hos ...

  4. python 天气预报 mysql_python + docker, 实现天气数据 从FTP获取以及持久化(二)-- python操作MySQL数据库...

    前言 在这一节中,我们主要介绍如何使用python操作MySQL数据库. 准备 MySQL数据库使用的是上一节中的docker容器 "test-mysql". Python 操作 ...

  5. python操作mysql中的表_带你了解什么是Python操作MySQL数据库

    写这篇文章主要是为了介绍Python操作MySQL数据库,并结合相应的实例带你更加深入了解.文中的代码实例很详细,对大家有一定的参考学习价值. 1.什么是pymysql? PyMySQL是在Pytho ...

  6. python操作mysql数据库练习

    python操作mysql数据库练习 本次练习是在windows下,mysql版本为5.7,python版本为2.7.5,集成环境为pycharm. 创建表时,enign在innodb下支持事务,其他 ...

  7. imooc的疯狂的蚂蚁的课程《Python操作MySQL数据库》 python3+pymysql模块来操作mysql数据库

    以下代码为imooc的疯狂的蚂蚁的课程<Python操作MySQL数据库>的python3版本的代码,使用的是pymysql模块来操作mysql数据库,代码与原课程有所改动,注意运行时需要 ...

  8. python操作mysql数据库用到的fetchone()函数和fetchall()函数

    在用python操作mysql数据库时,碰到了下面这两个函数,标记一下: fetchone() : 返回单个的元组,也就是一条记录(row),如果没有结果 则返回 None fetchall() : ...

  9. python操作mysql数据库实现增删改查

    Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. Python 数据库接口支持非常多的数据库,你可以选择适合你项目的数据库: ...

最新文章

  1. 第十二周项目一-实现复数类中的运算符重载(1)
  2. 帝豪gl车机系统降级_从拥有帝豪GL开始,出行的好伴侣,说说感受
  3. mysql移植海思_minigui在海思解决方案(hi3515芯片)上的移植过程(转)
  4. C++标准转换运算符static_cast
  5. MySQL执行原理,逻辑分层、更改数据库处理引擎
  6. 天才基本法_【书鱼扫文】天才基本法:天才与中国式青春
  7. SD/MMC相关寄存器的介绍
  8. MATLAB中的eps
  9. Redis 高级教程 Redis 安全(2)
  10. 如何为resin的jvm-default.log瘦身
  11. 手把手教你学DSP 28335学习笔记
  12. 推荐10本大数据领域必读的经典好书(火速收藏)
  13. 一个数如果恰好等于它的因子之和,这个数就成为“完数”。 例如,28的因子为1,2,4,7,14。而28=1+2+4+7+14,因此28是“完数”。编程找出1000之内的所有完数,并按下面格
  14. css中元素居中和文本居中
  15. 写专利的一点小小心得
  16. 在Ubuntu20.04运行VINS-Fusion
  17. 蓝桥杯 试题 算法训练 跳马 C++ 详解
  18. CCF认证2019031-小中大
  19. 区块链的未来:公链VS联盟链
  20. 跳过数据准备,下秒数据让飞书维格表

热门文章

  1. 485通讯的校验和_S7200 SMART通过MODBUS库与称重仪表通讯
  2. 基于51单片机的音乐播放器音乐盒仿真原理图方案设计
  3. Spark 调研报告
  4. BEVDet:High-Performance Multi-Camera3D Object Detection in Bird-Eye-View
  5. 亲身体验微软的 - 视窗梦幻桌面(DreamScene)
  6. 优秀的菜单栏日历软件:InstaCal mac版
  7. android媒体播放器课程设计,基于android的多媒体播放器课程设计报告.doc
  8. 汽车名词解释-发动机参数(2)
  9. 追风者变引领者:Horwin的技术攀爬
  10. 氮化物 ITO-Ag复合薄膜/Si—P(111)和Si—P(100)两种不同Si片做基底制备TiNx薄膜/立方氮化硼聚晶(PcBN)复合物