使用sqlite3 8张数据表和pyqt实现超市基本管理程序
时间: 2024-02-22 12:01:17 浏览: 27
好的,我可以为您提供一些指导。
首先,您需要安装SQLite3数据库和PyQt的Python模块。然后,您可以创建8个数据表来存储超市管理程序的基本信息,例如:商品、供应商、客户、订单、库存、销售、员工和工资。
以下是一个简单的示例代码,用于创建一个包含8个数据表的SQLite3数据库:
```python
import sqlite3
conn = sqlite3.connect('supermarket.db')
c = conn.cursor()
# 创建商品表
c.execute('''CREATE TABLE products
(id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
price REAL NOT NULL,
quantity INTEGER NOT NULL,
supplier_id INTEGER NOT NULL,
FOREIGN KEY (supplier_id) REFERENCES suppliers(id))''')
# 创建供应商表
c.execute('''CREATE TABLE suppliers
(id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
phone TEXT NOT NULL)''')
# 创建客户表
c.execute('''CREATE TABLE customers
(id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
phone TEXT NOT NULL)''')
# 创建订单表
c.execute('''CREATE TABLE orders
(id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL,
order_date TEXT NOT NULL,
total REAL NOT NULL,
FOREIGN KEY (customer_id) REFERENCES customers(id))''')
# 创建库存表
c.execute('''CREATE TABLE inventory
(id INTEGER PRIMARY KEY,
product_id INTEGER NOT NULL,
quantity INTEGER NOT NULL,
FOREIGN KEY (product_id) REFERENCES products(id))''')
# 创建销售表
c.execute('''CREATE TABLE sales
(id INTEGER PRIMARY KEY,
product_id INTEGER NOT NULL,
order_id INTEGER NOT NULL,
quantity INTEGER NOT NULL,
FOREIGN KEY (product_id) REFERENCES products(id),
FOREIGN KEY (order_id) REFERENCES orders(id))''')
# 创建员工表
c.execute('''CREATE TABLE employees
(id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
phone TEXT NOT NULL,
salary REAL NOT NULL)''')
# 创建工资表
c.execute('''CREATE TABLE salaries
(id INTEGER PRIMARY KEY,
employee_id INTEGER NOT NULL,
salary_month TEXT NOT NULL,
amount REAL NOT NULL,
FOREIGN KEY (employee_id) REFERENCES employees(id))''')
conn.commit()
conn.close()
```
接下来,您可以使用PyQt创建一个GUI界面,使用户可以添加、编辑和删除数据表中的记录。您可以使用PyQt提供的表格视图、列表视图和表单视图来显示和编辑数据。
以下是一个简单的示例代码,用于创建一个包含表格视图和表单视图的PyQt窗口:
```python
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QTableView, QFormLayout
from PyQt5.QtSql import QSqlDatabase, QSqlTableModel
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# 创建数据库连接
self.db = QSqlDatabase.addDatabase('QSQLITE')
self.db.setDatabaseName('supermarket.db')
self.db.open()
# 创建模型和视图
self.model = QSqlTableModel()
self.model.setTable('products')
self.model.select()
self.view = QTableView()
self.view.setModel(self.model)
# 创建表单
self.form = QWidget()
self.formLayout = QFormLayout()
self.nameLineEdit = QLineEdit()
self.priceLineEdit = QLineEdit()
self.quantityLineEdit = QLineEdit()
self.supplierLineEdit = QLineEdit()
self.formLayout.addRow(QLabel('Name:'), self.nameLineEdit)
self.formLayout.addRow(QLabel('Price:'), self.priceLineEdit)
self.formLayout.addRow(QLabel('Quantity:'), self.quantityLineEdit)
self.formLayout.addRow(QLabel('Supplier:'), self.supplierLineEdit)
self.submitButton = QPushButton('Submit')
self.submitButton.clicked.connect(self.submit)
self.formLayout.addWidget(self.submitButton)
self.form.setLayout(self.formLayout)
# 创建布局
self.layout = QVBoxLayout()
self.layout.addWidget(self.view)
self.layout.addWidget(self.form)
self.centralWidget = QWidget()
self.centralWidget.setLayout(self.layout)
self.setCentralWidget(self.centralWidget)
def submit(self):
# 插入数据
record = self.model.record()
record.setValue('name', self.nameLineEdit.text())
record.setValue('price', self.priceLineEdit.text())
record.setValue('quantity', self.quantityLineEdit.text())
record.setValue('supplier_id', self.supplierLineEdit.text())
self.model.insertRecord(-1, record)
self.model.submitAll()
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
```
这是一个非常基本的示例,您可以根据自己的需要自定义和扩展。祝您好运!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)