import pymysql
from pymysql.cursors import DictCursor
# coon = pymysql.connect(host='211.149.218.16',port=3306,user='jxz',passwd='123456',db='jxz',charset='utf8')
# cur = coon.cursor(DictCursor)#建立游标,仓库管理员,指定游标类型,返回字典
# sql='select * from stu limit 2,9;'
# # insert_sql = 'insert into stu VALUE (89,"nhy");'
# # cur.execute(insert_sql)
# cur.execute(sql)#执行sql语句
# for c in cur:#直接循环游标,每次循环的时候就是每一列的数据
# print(c)
# # res = cur.fetchall()#获取sql语句执行的结果,一次性全部获取
# # coon.commit()#提交
# # print(res[0][1])
# cur.close()
# coon.close()
def op_mysql(host,user,passwd,db,sql,charset='utf8',port=3306):
import pymysql
from pymysql.cursors import DictCursor
coon = pymysql.connect(host=host,user=user,passwd=passwd,db=db,charset=charset,port=port)
cur = coon.cursor(DictCursor)#指订返回数据的类型是字典
cur.execute(sql)
if sql.strip().startswith('select'):
res = cur.fetchall()
else:
coon.commit()
res = 'ok'
cur.close()
coon.close()
print(res)
return res