# coding=utf-8
import pymysql
import os
import configparser
"""
/*
@:param: python version:3.7
@:param: author Chen
@:param: Date: 2019.01.19
@:param:
@env:
beta
alpha
@db_name: see_db,ec_db
*/
"""
class EnvConfig(object):
def __init__(self,env,db_name):
config_dir = os.path.split(os.path.realpath(__file__))[0]
config_path = os.path.join(config_dir, "config.ini")
config = configparser.ConfigParser()
config.read(config_path)
self.ip = config.get(env,'ip')
self.user = config.get(env,'user')
self.pwd =config.get(env,'pwd')
self.db_name = config.get(env,db_name)
self.port = int(config.get(env,'port')) """
/*
cases:
t_job=GetConnectDb("see_db","beta") */
""" class GetConnectDb(EnvConfig):
def __init__(self,env,db_name):
EnvConfig.__init__(self,env,db_name)
ip=self.ip
port=self.port
user=self.user
pwd=self.pwd
db=self.db_name
self.connect=pymysql.connect(ip,port,user,pwd,db,charset='utf8')
self.cursor=self.connect.cursor()
try:
if self.cursor:
pass
except ConnectionError as e:
print(e) """
/*
@:param: sql Usage:select * xx t_job where pipelineId='6444df4555sa55dgd';
@:param: rows type is int ,get the first rows of data
*/
"""
def disconnect_db(self):
try:
self.cursor.close()
self.connect.close()
except ConnectionAbortedError as e:
try:
self.disconnect_db()
except ConnectionAbortedError as e:
print(e)
print(e) def exe_select(self,sql,rows=None):
try:
if rows == None:
try:
self.cursor.execute(sql)
res=self.cursor.fetchall()
return res
except SyntaxError as e:
print(e)
finally:
self.disconnect_db()
else:
try:
self.cursor.execute(sql)
res=self.cursor.fetchmany(rows)
return res
except SyntaxError as e:
print(e)
finally:
self.disconnect_db()
except Exception as e:
print(e) # /*
# @:param:
# Usage:
# when is self.execute(sql) Usage:
# sql = insert into t_job(column1,column2,column3)values(a,b,c)
# array=None
# when is self.executemany(sql,array) Usage:
# sql = insert into t_job(id,user,date)values(%s,%s,%s)
# array = [(1001,"dev",'2018-11-25-11-12-43'),(1002,"test",'2018-11-25-11-12-44')]
# @:param: rows type is int ,get the first rows of data
# */ def exe_insert(self,sql,array=None):
try:
if array is None:
try:
self.cursor.execute(sql)
self.connect.commit()
except SyntaxError as e:
self.connect.rollback()
print(e)
finally:
self.disconnect_db()
else:
try:
self.cursor.executemany(sql,array)
self.connect.commit()
except SyntaxError as e:
self.connect.rollback()
print(e)
finally:
self.disconnect_db()
except Exception as e:
print(e)
"""
/*
update sql :
@:param: sql
*/
""" def update_opt(self,sql):
try:
self.cursor.execute(sql)
self.connect.commit()
except Exception as e:
self.connect.rollback()
print(e)
finally:
self.disconnect_db()
def delete_opt(self,sql):
try:
self.cursor.execute(sql)
self.connect.commit()
except SyntaxError as e:
self.connect.rollback()
print(e)
finally:
self.disconnect_db()

pyhton 自动化pymysql操作mysqldb数据库增删改查封装的更多相关文章

  1. Python cx_oracle自动化操作oracle数据库增删改查封装,优化返回查询数据

    # coding=utf-8 import cx_Oracle import os import json os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_C ...

  2. python操作mysql数据库增删改查的dbutils实例

    python操作mysql数据库增删改查的dbutils实例 # 数据库配置文件 # cat gconf.py #encoding=utf-8 import json # json里面的字典不能用单引 ...

  3. Asp.Net操作MySql数据库增删改查

    Asp.Net操作MySql数据库增删改查,话不多说直接步入正题.git源码地址:https://git.oschina.net/gxiaopan/NetMySql.git  1.安装MySQL数据库 ...

  4. python2.7入门---操作mysql数据库增删改查

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

  5. Golang原生sql操作Mysql数据库增删改查

    Golang要操作mysql数据库,首先需要在当期系统配置GOPATH,因为需要使用go get命令把驱动包下载到GOPATH下使用. 首先配置好你的GOPATH,执行以下命令,下载安装mysql驱动 ...

  6. pymysql连接数据库,实现数据库增删改查

    1.数据库连接 # 创建连接 def create_conn(): import pymysql conn = pymysql.connect( host='localhost', port=3306 ...

  7. Node使用Mongoose操作MongoDB数据库——增删改查的实现

    当初刚出社会时就规划了下自己的职业生涯:先成为一名优秀的前端工程师,再成为一名全栈工程师(精通前端开发.后台开发和客户端开发),最后成为一名优秀的系统架构师.转眼间已经工作快三年,是时候迈出关键性的一 ...

  8. python3操作mysql数据库增删改查

    #!/usr/bin/python3 import pymysql import types db=pymysql.connect("localhost","root&q ...

  9. C#学习笔记(3)——操作sqlserver数据库增删改查

    说明(2017-5-25 16:29:35): 1. VS2010,视图->服务器资源管理器->数据连接->右键添加连接->服务器名(本机可以用点)->选择数据库-> ...

随机推荐

  1. IO实战-RandomAccessFile在本地实现伪断点续传

    准备:在磁盘中 准备一个目录文件 实现:将该文件复制到目标路径中,关掉程序,再重新打开可以在原位置继续复制. 需求如下: 过程中显示文件的拷贝的百分比 复制过程中关掉程序. 重新启动该程序时,若上次没 ...

  2. [LeetCode]Longest Palindromic Substring题解(动态规划)

    Longest Palindromic Substring: Given a string s, find the longest palindromic substring in s. You ma ...

  3. 再谈javascript函数节流

    之前写过但是不记得在哪了,今天同事要一个滑到页面底部加载更多内容的效果,又想起了这玩意儿,确实挺实用和常用的,谨此记之. 函数节流从字面上的意思就是节约函数的执行次数,其实现的主要思想是通过定时器阻断 ...

  4. 前端学习之路之CSS (四)

    Infi-chu: http://www.cnblogs.com/Infi-chu/ CSS盒子模型    概念:CSS盒模型本质上是一个盒子,封装周围的HTML元素,它包括:边距,边框,填充,和实际 ...

  5. gulp快速将css中的px替换成rem

    1.Gulp安装配置 1.全局安装gulp 1.1 安装 命令提示符执行cnpm install gulp -g; 1.2 查看是否正确安装:命令提示符执行gulp -v,出现版本号即为正确安装. 2 ...

  6. django开发博客(1) 入门

    现在正式开始博客开发 1.安装django1.4 如果你使用的是fedoraDVD版,安装时选择了web开发组建,这一步可以省略,因为它自带django环境 django下载地址 https://ww ...

  7. maven(14)-nexus仓库基本用法

    登录 启动nexus3,访问http://localhost:8081/  点击右上角sign in登录,默认用户名:admin  密码:admin123,登陆后可以点击右上角的admin,修改默认密 ...

  8. 封装网络请求并在wxml调用

    https://blog.csdn.net/qq_35713752/article/details/78109084 // url:网络请求的url method:网络请求方式 data:请求参数 m ...

  9. C#多线程顺序依赖执行控制

    在开发过程中,经常需要多个任务并行的执行的场景,同时任务之间又需要先后依赖的关系.针对这样的处理逻辑,通常会采用多线程的程序模型来实现. 比如A.B.C三个线程,A和B需要同时启动,并行处理,且B需要 ...

  10. kernel update

    CentOS/RHEL更新包:https://rhn.redhat.com/errata/RHSA-2017-1382.html yum makecache --更新源 yum update sudo ...