欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

三层架构之公共层 操作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)

相关标签: MySQLdb