1 Star 5 Fork 2

给点知识/Python操作Mysql 和Sqlite3

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ZMysql.py 3.06 KB
一键复制 编辑 原始数据 按行查看 历史
给点知识 提交于 2021-10-07 10:41 . add ZMysql.py.
# -*- coding:utf8 -*-
import pymysql
import re
import threading
lock = threading.Lock()
class ZMysql:
"""
auther:zhu
类用于连接mysql数据库
类创建即连接
类消失即关闭数据库
"""
def __init__(self,host='localhost',port = 3306,database="qnh",
user='root',password='',charset='utf8'):
"""
初始化方法
"""
self.host = host
self.port = port
self.database = database
self.user = user
self.password = password
self.charset = charset
self.__cursor = None
self.__connect = None
self.__get_mysql_cursor()
def __del__(self):
self.__close()
def __get_mysql_cursor(self):
"""
创建对象后直接获取游标即可使用
"""
data_dict = {
"host": self.host,
"port": self.port,
"database": self.database,
"user": self.user,
"password": self.password,
"charset": self.charset
}
# 连接数据库
try:
self.__connect = pymysql.connect(**data_dict)
self.__cursor = self.__connect.cursor()
except Exception as ex:
# print("-----------Host:%s Database:%s User:%s error!!-----------提示:"%(self.host,self.database,self.user),ex)
self.__cursor = None
else:
pass
# print("-----------Host:%s Database:%s 用户:%s success!!-----------"%(self.host,self.database,self.user))
# 执行sql语句
def execute_sql(self, sql,*args):
# 查询语句
if not self.__cursor:
return False
sql_del_pace = re.sub(' +','',sql,count=1)
# pace1 = sql.replace(" ","")
# print(sql_del_pace)
re_result = re.match("[^ ]*", sql_del_pace, re.I).group()
if re_result.lower()[:6] == "select":
lock.acquire()
self.__cursor.execute(sql,args=args)
lock.release()
# print("-----------Host:%s Database:%s User:%s action:%s param:%s Complete!-----------"%(self.host,self.database,self.user,sql,args))
return self.__cursor.fetchall()
else:
# 执行更新、插入、修改语句
lock.acquire()
self.__cursor.execute(sql,args=args)
self.__connect.commit()
lock.release()
# print("-----------Host:%s Database:%s User:%s action:%s param:%s Complete!-----------"%(self.host,self.database,self.user,sql,args))
return self.__cursor.lastrowid
def __close(self):
"""
关闭数据库连接
"""
self.__cursor.close()
self.__connect.close()
# print("-----------Host:%s Database:%s User:%s The database is down!!-----------"%(self.host,self.database,self.user))
# print("-----------The database is down!-----------")
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/gdzsfun/python-operation---mysql.git
git@gitee.com:gdzsfun/python-operation---mysql.git
gdzsfun
python-operation---mysql
Python操作Mysql 和Sqlite3
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385