first commit
This commit is contained in:
1
芳林公司项目/pytest/db/.pydio
Normal file
1
芳林公司项目/pytest/db/.pydio
Normal file
@@ -0,0 +1 @@
|
||||
cc3a78fe-06ea-426c-9cca-a81ac37a7140
|
||||
73
芳林公司项目/pytest/db/index.py
Normal file
73
芳林公司项目/pytest/db/index.py
Normal file
@@ -0,0 +1,73 @@
|
||||
import pymysql
|
||||
from config.index import MySqlConfig
|
||||
|
||||
class DataBaseHandle(object):
|
||||
''' 定义一个 MySQL 操作类'''
|
||||
def __init__(self):
|
||||
self.db = pymysql.connect(MySqlConfig['host'], MySqlConfig['username'],
|
||||
MySqlConfig['password'], MySqlConfig['dataBase'],
|
||||
MySqlConfig['port'], charset='utf8')
|
||||
def insertDB(self, sql):
|
||||
'插入数据库操作'
|
||||
|
||||
self.cursor = self.db.cursor()
|
||||
|
||||
try:
|
||||
# 执行sql
|
||||
self.cursor.execute(sql)
|
||||
# tt = self.cursor.execute(sql) # 返回 插入数据 条数 可以根据 返回值 判定处理结果
|
||||
# print(tt)
|
||||
self.db.commit()
|
||||
except:
|
||||
# 发生错误时回滚
|
||||
self.db.rollback()
|
||||
finally:
|
||||
self.cursor.close()
|
||||
|
||||
def deleteDB(self, sql):
|
||||
'操作数据库数据删除'
|
||||
self.cursor = self.db.cursor()
|
||||
|
||||
try:
|
||||
# 执行sql
|
||||
self.cursor.execute(sql)
|
||||
# tt = self.cursor.execute(sql) # 返回 删除数据 条数 可以根据 返回值 判定处理结果
|
||||
# print(tt)
|
||||
self.db.commit()
|
||||
except:
|
||||
# 发生错误时回滚
|
||||
self.db.rollback()
|
||||
finally:
|
||||
self.cursor.close()
|
||||
|
||||
def updateDb(self, sql):
|
||||
'更新数据库操作'
|
||||
self.cursor = self.db.cursor()
|
||||
|
||||
try:
|
||||
# 执行sql
|
||||
self.cursor.execute(sql)
|
||||
# tt = self.cursor.execute(sql) # 返回 更新数据 条数 可以根据 返回值 判定处理结果
|
||||
# print(tt)
|
||||
self.db.commit()
|
||||
except:
|
||||
# 发生错误时回滚
|
||||
self.db.rollback()
|
||||
finally:
|
||||
self.cursor.close()
|
||||
|
||||
def selectDb(self, sql):
|
||||
'数据库查询'
|
||||
self.cursor = self.db.cursor()
|
||||
try:
|
||||
self.cursor.execute(sql) # 返回 查询数据 条数 可以根据 返回值 判定处理结果
|
||||
data = self.cursor.fetchall() # 返回所有记录列表
|
||||
return data
|
||||
except:
|
||||
print('Error: unable to fecth data')
|
||||
finally:
|
||||
self.cursor.close()
|
||||
|
||||
def closeDb(self):
|
||||
'数据库连接关闭'
|
||||
self.db.close()
|
||||
Reference in New Issue
Block a user