首页 > 数据库技术 > 详细

优化MyDb

时间:2018-05-25 20:37:45      阅读:268      评论:0      收藏:0      [点我收藏+]
import pymysql
class MyDb(object): #新式类

def __del__(self):#析构函数
self.cur.close()
self.coon.close()
print(‘over...‘)

def __init__(self, #构造函数
host,user,passwd,db,
port=3306,charset=‘utf8‘):
try:
self.coon=pymysql.connect(
host=host,user=user,passwd=passwd,port=port,
charset=charset,db=db,autocommit=True#自动提交
)
except Exception as e:
print(数据库连接失败!%s‘%e)
else: #建立游标
self.cur=self.coon.cursor(cursor=pymysql.cursors.DictCursor)

def ex_sql(self,sql):
try:
self.cur.execute(sql)
except Exception as e:
print(‘sql语句有问题,%s‘%sql)
else:
self.res=self.cur.fetchall()
return self.res

my=MyDb(‘xx.xx.xx.xx‘,‘xxx‘,‘123456‘,‘xxx‘)
my.ex_sql(‘select * from stu;‘)
print(my.res)
print(我是最后一行代码...‘)










优化MyDb

原文:https://www.cnblogs.com/jiadan/p/9090172.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!