首页 > 数据库 >简单封装一下pymysql库

简单封装一下pymysql库

时间:2023-05-31 23:07:46浏览次数:43  
标签:封装 cur get kwargs pymysql mysqldb 简单 print data

 

 

简单封装一下pymysql库

my_pymysql.py

#!/bin/python
# -*- coding:utf-8 -*-
import pymysql
import numpy

def get_connect(**kwargs):
    mysqldb=pymysql.connect(host=kwargs.get('host'),
               user=kwargs.get('user'),
               password=kwargs.get('password'),
               port=kwargs.get('port'),
               database=kwargs.get('database'),
               charset=kwargs.get('charset'))
    return mysqldb

def get_exec_return_list(mysqldb, sql):
    mysqldb=mysqldb
    mysqldb_cur=mysqldb.cursor()
    mysqldb_cur.execute(sql)
    mysqldb.commit()
    mysqldb_data = mysqldb_cur.fetchall()
    #mysqldb_cur.close()
    #mysqldb.close()
    mysqldb_data = numpy.array(mysqldb_data)
    return mysqldb_data
def get_exec_return_list_and_close(mysqldb, sql):
    mysqldb=mysqldb
    mysqldb_cur=mysqldb.cursor()
    mysqldb_cur.execute(sql)
    mysqldb.commit()
    mysqldb_data = mysqldb_cur.fetchall()
    mysqldb_cur.close()
    mysqldb.close()
    mysqldb_data = numpy.array(mysqldb_data)
    return mysqldb_data
def get_exec(mysqldb, sql):
    mysqldb=mysqldb
    mysqldb_cur=mysqldb.cursor()
    mysqldb_cur.execute(sql)
    mysqldb.commit()
    mysqldb_data = mysqldb_cur.fetchall()
    #mysqldb_cur.close()
    #mysqldb.close()
    return mysqldb_data

def get_exec_and_close(mysqldb, sql):
    mysqldb=mysqldb
    mysqldb_cur=mysqldb.cursor()
    mysqldb_cur.execute(sql)
    mysqldb.commit()
    mysqldb_data = mysqldb_cur.fetchall()
    mysqldb_cur.close()
    mysqldb.close()
    return mysqldb_data

def data_format(sql_data):
    pass

__version__ = '1.0'

将my_pymysql.py   放到/home/user/pylib/目录

 

test.py简单使用页

#!/bin/python

import sys
sys.path.append("/home/user/pylib/")

import my_pymysql
import time


DB_CONFIG = {
    "host": '192.168.10.201',
    "port": 3306,
    "user": 'root',
    "password": 'root__',
    "database": 'testdatabase',
    "charset": 'utf8'
}


sql='''
select * from testtable 
'''
sql1='''
select * from testtable where id = '1'
'''
sql2='''
select * from testtable where id = '2'
'''

def get_mysql_con():
    return my_pymysql.get_connect(host=DB_CONFIG['host'], user=DB_CONFIG['user'], password=DB_CONFIG['password'], database=DB_CONFIG['database'], charset=DB_CONFIG['charset']);

def get_mysql_data():
    #mysqldb = get_mysql_con();
    mysqldb = my_pymysql.get_connect(**DB_CONFIG);
    a = my_pymysql.get_exec(mysqldb, sql)
    return a;


def alarm_data(res):
    strlen = len(res);
    if res :
        print("查询到对应数据,需要告警!");
    else :
        print("无数据,查询状态正常,无需告警!");


res = get_mysql_data()
print(res)
cur_date = time.strftime("%Y-%m-%d",time.localtime())
print(cur_date)
mysqldb = my_pymysql.get_connect(**DB_CONFIG)
print(mysqldb)
print(sql1)
print(sql2)
res1 = my_pymysql.get_exec(mysqldb, sql1)
res2 = my_pymysql.get_exec(mysqldb, sql2)
print(res1)
print(res2)

 

 

 

附:

import pymysql
conn = pymysql.connect(
    host='127.0.0.1',
    user='root',
    password='root__',
    database='sqlmonitor',
    port=3306,
    charset='utf8'
)
sql = 'select * from pay_get_out order by id desc limit 10;'
cur = conn.cursor()
cur.execute(sql)
results = cur.fetchall()
path = results[0][1]
print(path)
cur.close()

 

/home/daixiongwei/lib/my_sendsms.py

#!/bin/python
# -*- coding:utf-8 -*-
import requests
import json
import datetime
import os
import sys
import logging

#alarm_v5 = "http://10.192.219.13:58080/alarm/v5"
alarm_v5 = "http://10.192.219.12:58080/alarm/v5"

def send_smsv5(token, users, sendChannel, ttsVoice, smsMsg, wechatMsg, emailTitle, emailMsg):
    headers = {'Content-Type': "application/json;charset=utf-8"}
    batch_info = {"token": token,
                  "alertSystem":"自定义监控系统",
                  "businessLine":"业务平台部",
                  "type":2,
                  "ruleType":2,
                  "grade":2,
                  "sendChannel":sendChannel,
                  "userAccounts":users,
                  "ttsVoice":ttsVoice,
                  "smsMsg":smsMsg,
                  "wechatMsg":wechatMsg,
                  "emailTitle":emailTitle,
                  "emailMsg":emailMsg }
    print(batch_info)
    batch_info = json.dumps(batch_info)
    response=requests.post(alarm_v5,data=batch_info,headers=headers)
    print(response.text)

def send_smsv5_1(token, users, sendChannel, **kwargs):
    headers = {'Content-Type': "application/json;charset=utf-8"}
    batch_info = {"token": token,
                  "alertSystem":"自定义监控系统",
                  "businessLine":"业务平台部",
                  "type":2,
                  "ruleType":2,
                  "grade":2,
                  "sendChannel":sendChannel,
                  "userAccounts":users,
                  "ttsVoice":kwargs.get('ttsVoice'),
                  "smsMsg":kwargs.get('smsMsg'),
                  "wechatMsg":kwargs.get('wechatMsg'),
                  "emailTitle":kwargs.get('emailTitle'),
                  "emailMsg":kwargs.get('emailMsg') }
    print(batch_info)
    batch_info = json.dumps(batch_info)
    response=requests.post(alarm_v5,data=batch_info,headers=headers)
    print(response.text)

def send_smsv5_2(**kwargs):
    headers = {'Content-Type': "application/json;charset=utf-8"}
    batch_info = {"token": kwargs.get('token'),
                  "alertSystem":"自定义监控系统",
                  "businessLine":"业务平台部",
                  "type":2,
                  "ruleType":2,
                  "grade":2,
                  "sendChannel":kwargs.get('sendChannel'),
                  "userAccounts":kwargs.get('users'),
                  "ttsVoice":kwargs.get('ttsVoice'),
                  "smsMsg":kwargs.get('smsMsg'),
                  "wechatMsg":kwargs.get('wechatMsg'),
                  "emailTitle":kwargs.get('emailTitle'),
                  "emailMsg":kwargs.get('emailMsg')  }
    print(batch_info)
    batch_info = json.dumps(batch_info)
    response=requests.post(alarm_v5,data=batch_info,headers=headers)
    print(response.text)

 

标签:封装,cur,get,kwargs,pymysql,mysqldb,简单,print,data
From: https://blog.51cto.com/lenglingx/6390769

相关文章

  • 【React工作记录七十七】React+hook+ts+ant design封装一个input和select搜索的组件
    前言我是歌谣我有个兄弟巅峰的时候排名c站总榜19叫前端小歌谣曾经我花了三年的时间创作了他现在我要用五年的时间超越他今天又是接近兄弟的一天人生难免坎坷大不了从头再来歌谣的意志是永恒的放弃很容易但是坚持一定很酷微信公众号前端小歌谣需求分析首先我们需要实现......
  • 【React工作记录七十八】React+hook+ts+ant design封装一个table的组件
    前言我是歌谣我有个兄弟巅峰的时候排名c站总榜19叫前端小歌谣曾经我花了三年的时间创作了他现在我要用五年的时间超越他今天又是接近兄弟的一天人生难免坎坷大不了从头再来歌谣的意志是永恒的放弃很容易但是坚持一定很酷微信公众号前端小歌谣需求分析在前端项目中最常......
  • 【socket】服务端与客户端简单代码
    1、C实现代码ExampleofClient-ServerPrograminC(UsingSocketsandTCP)|ProgrammingLogic  2、问题调试经验--缺少头文件导致的段错误-戴安澜式编程-博客园(84条消息)【C语言】warning:implicitdeclarationoffunction‘xxx’[-Wimplicit-function-de......
  • Docker 常用命令简单总结
    1、安装docker1.1、安装docker:sudoapt-getinstall-ydocker.io1.2、启动docker服务:systemctlstartdocker1.3、设置开机启动:systemctlenabledocker1.4、查看docker版本:docker--version1.5、查看Dockercpu/内存占用状态:dockerstats--help1.6、查看Docker状态:systemctlsta......
  • 一个简单的科普-延迟与RT时间
    一个简单的科普-延迟与RT时间背景发现稍微一复杂就没人看.这次像是写一个简单的科普文章.主要说一下网络延迟还有网络的响应时间.这里想通过一个题目进行引申.如果Skylink全球商用:中国与美国之间的网络是走海底光纤延迟低,还是走skylink延迟低?关于时延的理解我认......
  • HDU4259(简单群置换)
    题目:DoubleDealing#include<iostream>#include<string.h>#defineLLlonglongconstintN=1005;intn,k;inta[N][N];boolf[N];intnum[N];LLgcd(LLa,LLb){returnb?gcd(b,a%b):a;}intmain(){inti,j;while(std::cin>......
  • 使用NPOI组件简单实现导出
    首先我们在NuGet程序包管理中下载NPOI组件目前版本稳定的是:2.4.1这里可以看一下NPOI下载完成后生成的四个引用,其作用然后废话也不多说了,直接上代码然后我们在项目中进行引用usingNPOI.SS.UserModel;usingNPOI.XSSF.UserModel;usingNPOI.HSSF.UserModel;点......
  • 02.简单工厂
    定义提供一个创建对象实例的功能,而无需关心具体实现。创建实例的类型可以是接口、抽象类,也可以是具体的类。代码示例///<summary>///工厂类,用来创建api对象///</summary>publicclassFactory{publicstaticIApiCreateApi(intcondition){IApiapi......
  • 最大熵模型原理——选择符合所有事实,在其他方面尽可能均匀分布(熵最大)的模型,其实思想很
    1.熵与最大熵原理熵是随机变量不确定性的度量,不确定性越大,熵值就越大;若随机变量退化成定值,熵为0。均匀分布是“最不确定”的分布假设离散随机变量X的概率分布为P(x),则其熵为:联合熵和条件熵两个随机变量的X,Y的联合分布,可以形成联合熵,用H(X,Y)表示条件熵H(X|Y)=H(X,Y)-H(Y) 相对......
  • mDNS原理的简单理解——每个进入局域网的主机,如果开启了mDNS服务的话,都会向局域网内的
    MDNS协议介绍mDNSmulticastDNS,使用5353端口,组播地址224.0.0.251。在一个没有常规DNS服务器的小型网络内,可以使用mDNS来实现类似DNS的编程接口、包格式和操作语义。MDNS协议的报文与DNS的报文结构相同,但有些字段对于MDNS来说有新的含义。每个进入局域网的主机,如果开启了mDNS服......