1 def my_db(host,user,passwd,db,sql,port=3306,charset='utf8'): 2 import pymysql 3 coon = pymysql.connect(user=user,host=host,port=port,passwd=passwd, 4 db=db, 5 charset=charset) 6 cur = coon.cursor()#建立游标 7 cur.execute(sql)#执行sql 8 if sql.strip()[:6].upper()=='SELECT': 9 res = cur.fetchall()10 else:11 coon.commit()12 res = 'ok'13 cur.close()14 coon.close()15 return res