MySQLdb模块 类操作方法

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from day5 import conf
import MySQLdb


class MysqlHepler(object):
    def __init__(self):
        self.conn = MySQLdb.connect(**conf.conn_dict)
        self.cur = self.conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
        self.cur.execute(sql,parmas)

    def Select_All(self,sql,parmas):
        refetch = self.cur.fetchall()
        self.cur.close()
        self.conn.close()
        return refetch

    def Execute(self,sql,parmas):
        self.conn.commit()
        self.cur.close()
        self.conn.close()



p1 = MysqlHepler()
sql = 'select * from admin where id = %s'
parmas = (3,)
p1.Select_All(sql,parmas)




#######################################################

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from day5.utility.sql_helpler import MysqlHepler
import MySQLdb

class AdminClass(object):
def __init__(self):
self.helper = MysqlHepler()

def SelectIp(self,id):
sql = 'select * from admin where id = %s'
parmas = (id,)
return self.helper.Select_All(sql,parmas)


#########################################################

conn_dict = dict(host='172.16.202.182',user='fengjian',passwd='123456',db='fengjian')




原文地址:https://www.cnblogs.com/fengjian2016/p/5251188.html