1 Star 0 Fork 3

OPSTime/cmdb-flask

forked from 施超/flask_cmdb 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
mysql_tools.py 1.46 KB
一键复制 编辑 原始数据 按行查看 历史
施超 提交于 2021-06-04 11:23 . 提交学习文件
#!/usr/bin/env python
# _*_ coding: utf-8 _*_
# Author:shichao
# File: .py
import pymysql
# 封装连接
def getConnect():
try:
connection = pymysql.connect(
host="121.4.77.138",
user="cmdb",
password="Aa@963852",
database="cmdb",
port=3306)
return connection
except Exception as e:
print(e)
def selectByParameters(sql, params=None):
try:
connection = getConnect()
cursor = connection.cursor(pymysql.cursors.DictCursor)
cursor.execute(sql,params)
fc = cursor.fetchall()
return fc
except Exception as e:
print(e)
finally:
try:
cursor.close()
except Exception as e:
print(e)
try:
connection.close()
except Exception as e:
print(e)
def updateByParameters(sql, params=None):
try:
connection = getConnect()
cursor = connection.cursor(pymysql.cursors.DictCursor)
count = cursor.execute(sql,params)
connection.commit()
return count
except Exception as e:
connection.rollback()
print(e)
finally:
try:
cursor.close()
except Exception as e:
print(e)
try:
connection.close()
except Exception as e:
print(e)
if __name__ == '__main__':
sql = "select * from servers"
result = selectByParameters( sql )
print(result)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/OPSTime/cmdb.git
git@gitee.com:OPSTime/cmdb.git
OPSTime
cmdb
cmdb-flask
master

搜索帮助