脚本中使用mysql连接
定义一个装饰器 import pymysql def ana_r(func): def conn(*args,**kwargs): conn = pymysql.connect(host='172.16.17.10', user='admin_sa', password='pwd4sa#2017', database='analytics',port=3841, charset='utf8') db = conn.cursor() result = func(db,*args,**kwargs) conn.close() return result return conn 然后引入
from mysql_conn import ana_r
@ana_r
def test(db):
sql='select * from analytics.apm_jvm_mem ORDER by datetime limit 100'
row=db.execute(sql)
print(row)
test()
django中使用
import pymysql def jvm_monitor(request): conn = pymysql.connect(host='172.16.17.10', user='sunkedong', password='123456', database='analytics', port=3841, charset='utf8') db = conn.cursor() sql = 'select * from analytics.apm_jvm_mem ORDER by datetime limit 100' row = db.execute(sql) return render(request,'jvm_monitor.html',locals())