#-*-coding:GBK -*-
import pymysql
from pymysql.constants import CLIENT
class MYSQLHelpDict:
def __init__(self,host,user,pwd,db,port):
self.host = host
self.user = user
self.pwd = pwd
self.db = db
self.port = port
self.conn = pymysql.connect(host=self.host,user=self.user,password=self.pwd,database=self.db,charset="utf8",port=self.port, client_flag=CLIENT.MULTI_STATEMENTS)
self.cur = self.conn.cursor(cursor=pymysql.cursors.DictCursor)
def __GetConnect(self):
if not self.db:
raise(NameError,"没有设置数据库信息")
if not self.cur:
raise(NameError,"连接数据库失败")
else:
return self.cur
def ExecQuery(self,sql):
cur = self.__GetConnect()
cur.execute(sql)
resList = cur.fetchall()
return resList
def ExecNonQuery(self,sql):
cur = self.__GetConnect()
cur.execute(sql)
self.conn.commit()
def ExecClose(self):
#self.cur.close()
self.conn.close()
if __name__ == '__main__':
ms = MYSQLHelpDict.MYSQLHelp(host='127.0.0.1', user='root', pwd='123456', db='dnma,e',port=3306)
sql=" select * from student where username='张三'"
datas=ms.ExecQuery(sql)
ms.ExecClose()
print(datas)
07-20
406
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)