三层架构之公共层 操作mysql(在utility里面写)
程序员文章站
2024-02-10 13:04:04
...
import MySQLdb
class MySqlHelper(object):
def __init__(self):
pass
def Get_Dict(self,sql,params):
conn = MySqldb.connect(host='127.0.0.1',user='alex',passwd='123456',db='test',charset='utf-8')
cur = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)
reCount = cur.execute(sql,params)
data = cur.fetchall()
cur.close()
conn.close()
return data
def Get_one(self,sql,params):
conn = MySqldb.connect(host='127.0.0.1',user='alex',passwd='123456',db='test',charset='utf-8')
cur = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)
reCount = cur.execute(sql,params)
data = cur.fetchone()
cur.close()
conn.close()
return data
#下面的语句一般不会在公共层里面写
#helper = MySqlHelper()
#sql = 'select * from admin where id > %s'
#params = (1,)
#helper.Get_one(sql,params)
上一篇: oracle 中函数,游标的使用
下一篇: Python MySQLdb模块