标签:har host 数据 import __del__ passwd rom port fetch
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(‘我是最后一行代码...‘)
标签:har host 数据 import __del__ passwd rom port fetch
原文地址:https://www.cnblogs.com/jiadan/p/9090172.html