您的位置:首页 > 数据库 > MySQL

MySQLdb模块 类操作方法

2016-03-07 17:16 411 查看
#!/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')


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: