forked from PyQt5/PyQt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SqlQuery.py
149 lines (132 loc) · 5.13 KB
/
SqlQuery.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2018年5月15日
@author: Irony
@site: https://pyqt5.com , https://github.com/892768447
@email: [email protected]
@file: SqlQuery
@description:
"""
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QWidget, QMessageBox, QTableWidgetItem
from sqlalchemy.engine import create_engine
from sqlalchemy.ext.declarative.api import declarative_base
from sqlalchemy.orm.session import sessionmaker
from sqlalchemy.sql.expression import and_
from sqlalchemy.sql.schema import Column
from sqlalchemy.sql.sqltypes import Integer, Text
from Lib.mainui import Ui_Form
__Author__ = """By: Irony
QQ: 892768447
Email: [email protected]"""
__Copyright__ = "Copyright (c) 2018 Irony"
__Version__ = "Version 1.0"
# engine = create_engine('mysql+mysqldb://root@localhost:3306/tourist?charset=utf8')
engine = create_engine('sqlite:///Data/data.sqlite3', echo=True) # echo 表示开启命令显示
Base = declarative_base()
class Tourist(Base):
__tablename__ = 'tourist'
id = Column(Integer, primary_key=True)
name = Column(Text)
license = Column(Text)
flightnumber = Column(Text)
flightdate = Column(Text)
seatnumber = Column(Text)
boardingport = Column(Text)
no = Column(Text)
departurestation = Column(Text)
destinationstation = Column(Text)
class Window(QWidget, Ui_Form):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
self.setupUi(self)
# sql的拼接字段
self.sql = {}
# 数据库连接
self.session = sessionmaker(bind=engine)()
@pyqtSlot()
def on_pushButtonQuery_clicked(self):
"""查询按钮"""
self.applyName()
self.applySeat()
self.applyLicense()
self.applyPort()
if not self.sql:
return QMessageBox.warning(self, '提示', '没有进行任何输入')
# 清空数据
self.tableWidget.clear()
# 重新设置表头
self.tableWidget.setHorizontalHeaderLabels(
['编号', '姓名', '证件号', '航班号', '航班日期', '座位号', '登机口', '序号', '出发地', '目的地'])
# 根据选择的字段进行并列查询
rets = self.session.query(Tourist).filter(
and_(*(key == value for key, value in self.sql.items()))).all()
if not rets:
return QMessageBox.information(self, '提示', '未查询到结果')
self.tableWidget.setRowCount(len(rets))
# 根据查询结果添加到表格中
for row, tourist in enumerate(rets):
self.tableWidget.setItem(row, 0, QTableWidgetItem(str(tourist.id)))
self.tableWidget.setItem(
row, 1, QTableWidgetItem(str(tourist.name)))
self.tableWidget.setItem(
row, 2, QTableWidgetItem(str(tourist.license)))
self.tableWidget.setItem(
row, 3, QTableWidgetItem(str(tourist.flightnumber)))
self.tableWidget.setItem(
row, 4, QTableWidgetItem(str(tourist.flightdate)))
self.tableWidget.setItem(
row, 5, QTableWidgetItem(str(tourist.seatnumber)))
self.tableWidget.setItem(
row, 6, QTableWidgetItem(str(tourist.boardingport)))
self.tableWidget.setItem(row, 7, QTableWidgetItem(str(tourist.no)))
self.tableWidget.setItem(
row, 8, QTableWidgetItem(str(tourist.departurestation)))
self.tableWidget.setItem(
row, 9, QTableWidgetItem(str(tourist.destinationstation)))
def applyName(self):
"""姓名"""
if not self.checkBoxName.isChecked():
if Tourist.name in self.sql:
# 移除
self.sql.pop(Tourist.name)
# 更新或添加到字典里
else:
self.sql[Tourist.name] = self.lineEditName.text().strip()
def applySeat(self):
"""座位号"""
if not self.checkBoxSeat.isChecked():
if Tourist.seatnumber in self.sql:
# 移除
self.sql.pop(Tourist.seatnumber)
# 更新或添加到字典里
else:
self.sql[Tourist.seatnumber] = self.lineEditSeat.text().strip()
def applyLicense(self):
"""证件号"""
if not self.checkBoxLicense.isChecked():
if Tourist.license in self.sql:
# 移除
self.sql.pop(Tourist.license)
# 更新或添加到字典里
else:
self.sql[Tourist.license] = self.lineEditLicense.text().strip()
def applyPort(self):
"""登机口"""
if not self.checkBoxPort.isChecked():
if Tourist.boardingport in self.sql:
# 移除
self.sql.pop(Tourist.boardingport)
# 更新或添加到字典里
else:
self.sql[Tourist.boardingport] = self.lineEditPort.text().strip()
if __name__ == '__main__':
import sys
import cgitb
sys.excepthook = cgitb.Hook(1, None, 5, sys.stderr, 'text')
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())