python执行数据库的一个简单函数

 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

 

 
posted @ 2018-04-26 11:21  荒古、潇潇  阅读(972)  评论(0编辑  收藏  举报