pyhton 自动化pymysql操作mysqldb数据库增删改查封装
# 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数据库增删改查封装的更多相关文章
- Python cx_oracle自动化操作oracle数据库增删改查封装,优化返回查询数据
# coding=utf-8 import cx_Oracle import os import json os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_C ...
- python操作mysql数据库增删改查的dbutils实例
python操作mysql数据库增删改查的dbutils实例 # 数据库配置文件 # cat gconf.py #encoding=utf-8 import json # json里面的字典不能用单引 ...
- Asp.Net操作MySql数据库增删改查
Asp.Net操作MySql数据库增删改查,话不多说直接步入正题.git源码地址:https://git.oschina.net/gxiaopan/NetMySql.git 1.安装MySQL数据库 ...
- python2.7入门---操作mysql数据库增删改查
Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口.Python 数据库接口支持非常多的数据库,你可以选择适合你项目的数据库: G ...
- Golang原生sql操作Mysql数据库增删改查
Golang要操作mysql数据库,首先需要在当期系统配置GOPATH,因为需要使用go get命令把驱动包下载到GOPATH下使用. 首先配置好你的GOPATH,执行以下命令,下载安装mysql驱动 ...
- pymysql连接数据库,实现数据库增删改查
1.数据库连接 # 创建连接 def create_conn(): import pymysql conn = pymysql.connect( host='localhost', port=3306 ...
- Node使用Mongoose操作MongoDB数据库——增删改查的实现
当初刚出社会时就规划了下自己的职业生涯:先成为一名优秀的前端工程师,再成为一名全栈工程师(精通前端开发.后台开发和客户端开发),最后成为一名优秀的系统架构师.转眼间已经工作快三年,是时候迈出关键性的一 ...
- python3操作mysql数据库增删改查
#!/usr/bin/python3 import pymysql import types db=pymysql.connect("localhost","root&q ...
- C#学习笔记(3)——操作sqlserver数据库增删改查
说明(2017-5-25 16:29:35): 1. VS2010,视图->服务器资源管理器->数据连接->右键添加连接->服务器名(本机可以用点)->选择数据库-> ...
随机推荐
- background-position为什么会出现负值?
上篇文章讲到了雪碧图,其中小机器人抖腿的动作设置了图片的background-position:-640px 循环到-1200px,那么这个数值是如何得出来的?下面具体分析一下如何计算backgrou ...
- drupal中安装CKEditor文本编辑器,并配置图片上传功能 之 方法二
drupal中安装CKEditor文本编辑器,并配置图片上传功能 之 方法一 中介绍了ckeditor的安装和配置方法,其实还有另一种新方法,不用IMCE模块. 不过需要ckfinder的JS库,可以 ...
- android 获取http请求json数据
package com.my.gethttpjsondata; import java.io.BufferedReader;import java.io.ByteArrayOutputStream;i ...
- method invocation
package method.invocation; public class MethodInvocation { public static void main(String[] args) { ...
- python oop常用术语 继承 多态 封装
面向对象优点 1.通过封装明确了内外 2.通过继承+多态在语言层面支持了归一化设计 抽象/实现 抽象指对现实世界问题和实体的本质表现,行为和特征建模,建立一个相关的子集,可以用于 绘程序结构,从而实现 ...
- flask代码统计作业
用户表: create table userInfo( id int not null unique auto_increment, name )not null, password ) not nu ...
- 为什么使用.Net Core, Asp.net Core以及部署到云端
一,基于微软技术平台开发的应用项目,如果有以下的需求和潜在需求,建议使用.Net Core和Asp.net core技术架构: 1. 跨平台支持 2. 采用微服务架构 3. 使用Docker容器 4. ...
- python基础——操作系统简介
不同应用领域的主流操作系统 l 桌面操作系统 l 服务器操作系统 l 嵌入式操作系统 l 移动设备操作系统 桌面操作系统 Windows系列 用户群体很大 MacOS 适合于开发人员 Linu ...
- Python3.x urlib包
在Python3.x中,我们可以使用urlib这个组件抓取网页,urllib是一个URL处理包,这个包中集合了一些处理URL的模块,如下: 1.urllib.request模块是用来打开和读取URLs ...
- .NET的那些事儿(9)——C# 2.0 中用iTextSharp制作PDF(基础篇) .
该文主要介绍如何借助iTextSharp在C# 2.0中制作PDF文件,本文的架构大致按照iTextSharp的操作文档进行翻译,如果需要查看原文,请点击一下链接:http://itextsharp. ...