1 Star 0 Fork 15

logicjwell/python-dbhelper

forked from fangzheng/python-dbhelper 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
dbutils.py 1.69 KB
一键复制 编辑 原始数据 按行查看 历史
zfang 提交于 2019-04-19 09:45 . fix: add setup.py 安装文件
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
@version : v1.0
@author : fangzheng
@contact : zfang@hillinsight.com
@software : PyCharm
@filename : dbutils.py
@create time: 2019/3/27 10:12
@describe :
@use example: python dbutils.py [param1 param2]
-------------------------------------------------
"""
from logger import Logger
import traceback
log = Logger(name='dbutils')
class DBConfig:
def __init__(self, dictionary):
for k, v in dictionary.items():
setattr(self, k, v)
if not hasattr(self,'charset'):
self.charset = 'utf8'
def select(connect, sql, param=None, size=None):
"""
Query data
:param sql:
:param param:
:param size: Number of rows of data you want to return
:return:
"""
cur = connect.cursor()
rows = None
try:
cur.execute(sql, param)
if size:
rows = cur.fetchmany(size)
else:
rows = cur.fetchall()
except Exception as e:
connect.rollback()
log.error(traceback.format_exc())
log.error("[sql]:{} [param]:{}".format(sql, param))
finally:
cur.close()
return rows
def execute(connect, sql, param=None):
"""
exec DML:INSERT、UPDATE、DELETE
:param sql: dml sql
:param param: string|list
:return: Number of rows affected
"""
cnt = 0
try:
cur = connect.cursor()
cnt = cur.execute(sql, param)
connect.commit()
except Exception as e:
connect.rollback()
log.error(traceback.format_exc())
log.error("[sql]:{} [param]:{}".format(sql, param))
finally:
cur.close()
return cnt
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/logicjwell/python-dbhelper.git
git@gitee.com:logicjwell/python-dbhelper.git
logicjwell
python-dbhelper
python-dbhelper
master

搜索帮助