首先要下载安装MySQLdb http://sourceforge.net/projects/mysql-python/
win7 64位安的时候有点儿费劲,只找到了1.2.3版本的,但是可用 http://www.codegood.com/archives/129
下面进入正题,三层架构我也没怎么学过,大概凭理解和看别人的代码弄出来一个,不知道理解的对不对,还望高手指教
代码的源码在这里:http://download.csdn.net/detail/huang369509940/6851801
先是数据库层面的:
MySqlHelper:
import MySQLdb
class MySqlHelper:
'''
实现数据库具体操作的类
'''
def __init__(self, SQLINFO):
try:
import MySQLdb
except ImportError,e:
raise e
try:
host=SQLINFO['host']
user=SQLINFO['user']
passwd=SQLINFO['password']
port=SQLINFO['port']
db=SQLINFO['db']
charset=SQLINFO['charset']
self.conn=MySQLdb.connect(host=host,user=user,db=db,passwd=passwd,port=port,charset=charset) #连接
self.cursor=self.conn.cursor() #游标对象
except Exception,e:
raise e
'''
函数名:Finish
输入:无
功能:提交数据库操作并关闭数据库连接
输出:无
'''
def Finish(self):
try:
self.conn.commit()
self.conn.close()
self.cursor.close()
except Exception,e:
raise e
'''
函数名:CreateDB
输入:数据库名称
功能:创建数据库
输出:无
'''
def CreateDB(self,db):
try:
self.cursor.execute('create database %s' %db)
self.Finish()
except Exception,e:
raise e
'''
函数名:ExecuteNonQuery
输入:待执行的非查询SQL语句
功能:执行sql语句
输出:受影响的行数
'''
def ExecuteNoQuery(self,SQLstring):
try:
count=self.cursor.execute(SQLstring)
self.Finish()
return count
except Exception,e:
raise e
return -1
'''
函数名:ExecuteQuery
输入:待执行的查询SQL语句
功能:执行sql语句
输出:查询的结果集
'''
def ExecuteQuery(self,SQLstring):
try:
self.cursor.execute(SQLstring)
result=self.cursor.fetchall() #接收全部的返回结果行
self.Finish()
return result
except Exception,e:
raise e
return None
'''
函数名:GetStringForList
输入:待合并的列表
功能:把列表进行合并
输出:合并后的字符串
'''
def GetStringForList(list):
str=''
for elem in list:
str+=elem+' '
return str
学生类:
class student:
'''
学生表
'''
def __init__(self,ID=None,Name=None,age=None):
self.ID=ID
self.Name=Name
self.age=age
然后是业务逻辑层的:
from Helper.MySqlHelper import *
from student import student
class Operate_stu:
'''
操作学生表
'''
def __init__(self,SQLINFO):
self.SQLINFO=SQLINFO
def Insert(self,stu):
s=[]
SqlBuilder=[]
SqlBuilder.append('insert into student')
SqlBuilder.append("values(%s,'%s',%s)" %(stu.ID,stu.Name,stu.age))
cmd=GetStringForList(SqlBuilder)
result=MySqlHelper(self.SQLINFO).ExecuteNoQuery(cmd)
if result==():
return s
return result
def GetAll(self):
s=[]
cmd='select * from student'
result=MySqlHelper(self.SQLINFO).ExecuteQuery(cmd)
if result==():
return s
return result
def GetAllinfoById(self,ID):
stu=student()
SqlBuilder=[]
SqlBuilder.append('select ID,Name,age')
SqlBuilder.append('from student where ID=%s' %ID)
cmd=GetStringForList(SqlBuilder)
result=MySqlHelper(self.SQLINFO).ExecuteQuery(cmd)
if result==():
return stu
result=result[0]
stu.ID=ID
stu.Name=result[1]
stu.age=result[2]
return stu
最后:
数据库和表都是运行时创建的,然后插了两条数据,最后查询数据
from Helper.MySqlHelper import *
from Operate import *
if __name__ == '__main__':
SQLINFO={'host':'localhost','user':'root','password':'111111','port':3306,'db':'','charset':'utf8'}
dbname='test'
MySqlHelper(SQLINFO).CreateDB(dbname) #创建数据库
SQLINFO['db']=dbname
MySqlHelper(SQLINFO).ExecuteNoQuery('create table Student(ID int primary key,Name varchar(20),age int)') #创建表
#插入数据
stu=student(1,'test',21)
Operate_stu(SQLINFO).Insert(stu)
stu.ID=2
stu.Name='Jo'
stu.age=20
Operate_stu(SQLINFO).Insert(stu)
result=Operate_stu(SQLINFO).GetAll()
for r in result:
print 'ID:%s,Name:%s,age:%s' %(r[0],r[1],r[2])
stu=Operate_stu(SQLINFO).GetAllinfoById(2)
print 'ID:%s,Name:%s,age:%s' %(stu.ID,stu.Name,stu.age)