使用python+pyqt+mysql编写的python三层架构项目
时间: 2023-11-23 20:05:56 浏览: 72
三层架构项目
Python三层架构项目通常分为三个层次:表示层、业务逻辑层和数据访问层。其中表示层主要负责与用户交互,包括用户界面的设计和展示;业务逻辑层主要负责实现系统的具体业务逻辑;数据访问层主要负责与数据库进行交互,包括数据的增删改查等操作。
使用Python+PyQt+MySQL编写的三层架构项目示例代码如下:
表示层(View):
```python
# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableView
from PyQt5.QtSql import QSqlDatabase, QSqlQuery, QSqlTableModel
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setWindowTitle("三层架构项目示例")
# 连接MySQL数据库
db = QSqlDatabase.addDatabase('QMYSQL')
db.setHostName('localhost')
db.setDatabaseName('test')
db.setUserName('root')
db.setPassword('')
if db.open():
print('数据库连接成功')
else:
print('数据库连接失败')
# 创建数据表模型
self.model = QSqlTableModel()
self.model.setTable("student")
self.model.setEditStrategy(QSqlTableModel.OnManualSubmit)
self.model.select()
# 创建表格视图
self.tableView = QTableView()
self.tableView.setModel(self.model)
self.setCentralWidget(self.tableView)
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
```
业务逻辑层(Controller):
```python
# -*- coding: utf-8 -*-
from PyQt5.QtSql import QSqlDatabase, QSqlQuery
class StudentController:
def __init__(self):
# 连接MySQL数据库
db = QSqlDatabase.addDatabase('QMYSQL')
db.setHostName('localhost')
db.setDatabaseName('test')
db.setUserName('root')
db.setPassword('')
if db.open():
print('数据库连接成功')
else:
print('数据库连接失败')
def add_student(self, name, age):
query = QSqlQuery()
query.prepare("INSERT INTO student (name, age) VALUES (?, ?)")
query.addBindValue(name)
query.addBindValue(age)
if query.exec_():
print('添加学生成功')
return True
else:
print('添加学生失败')
return False
def delete_student(self, id):
query = QSqlQuery()
query.prepare("DELETE FROM student WHERE id = ?")
query.addBindValue(id)
if query.exec_():
print('删除学生成功')
return True
else:
print('删除学生失败')
return False
def update_student(self, id, name, age):
query = QSqlQuery()
query.prepare("UPDATE student SET name = ?, age = ? WHERE id = ?")
query.addBindValue(name)
query.addBindValue(age)
query.addBindValue(id)
if query.exec_():
print('更新学生成功')
return True
else:
print('更新学生失败')
return False
def get_student_list(self):
query = QSqlQuery()
query.exec_("SELECT * FROM student")
student_list = []
while query.next():
student = {
'id': query.value(0),
'name': query.value(1),
'age': query.value(2)
}
student_list.append(student)
return student_list
```
数据访问层(Model):
```python
# -*- coding: utf-8 -*-
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
Base = declarative_base()
class Student(Base):
__tablename__ = 'student'
id = Column(Integer, primary_key=True)
name = Column(String(50), nullable=False)
age = Column(Integer, nullable=False)
class StudentModel:
def __init__(self):
# 连接MySQL数据库
engine = create_engine('mysql://root:@localhost/test?charset=utf8')
Session = sessionmaker(bind=engine)
self.session = Session()
def add_student(self, name, age):
student = Student(name=name, age=age)
self.session.add(student)
self.session.commit()
def delete_student(self, id):
student = self.session.query(Student).filter_by(id=id).first()
self.session.delete(student)
self.session.commit()
def update_student(self, id, name, age):
student = self.session.query(Student).filter_by(id=id).first()
student.name = name
student.age = age
self.session.commit()
def get_student_list(self):
student_list = []
for student in self.session.query(Student).all():
student_dict = {
'id': student.id,
'name': student.name,
'age': student.age
}
student_list.append(student_dict)
return student_list
```
通过以上示例代码,我们可以实现一个简单的Python三层架构项目,并且可以实现基本的数据的增删改查操作。
阅读全文