import
sqlite3
from
PyQt5.QtWidgets
import
QApplication, QWidget, QVBoxLayout, QTableWidget, QTableWidgetItem, QPushButton, QHBoxLayout, QLabel, QLineEdit, QComboBox, QScrollArea, QMessageBox
from
PyQt5.QtCore
import
Qt
page_size
=
28
current_page
=
0
def
query_data(page):
try
:
conn
=
sqlite3.connect(
'C:/Users/TPM/Desktop/VCMDataII.db'
)
cursor
=
conn.cursor()
offset
=
page
*
page_size
selected_option1
=
option_combo1.currentText()
selected_option2
=
option_combo2.currentText()
query
=
f
"SELECT * FROM T_LV2 WHERE Attribute3 LIKE '%{selected_option2}%' AND BatchId LIKE '%{selected_option1}%' LIMIT {page_size} OFFSET {offset};"
print
(f
"Executing query: {query}"
)
cursor.execute(query)
results
=
cursor.fetchall()
conn.close()
table.clearContents()
table.setRowCount (
0
)
if
len
(results) >
0
:
for
row_num, row_data
in
enumerate
(results):
table.insertRow(row_num)
for
col_num, data
in
enumerate
(row_data):
item
=
QTableWidgetItem(
str
(data))
item.setTextAlignment(Qt.AlignCenter)
table.setItem(row_num, col_num, item)
total_count
=
len
(results)
/
28
count_label.setText(f
"统计托数:{total_count}"
)
else
:
QMessageBox.information(widget,
"提示"
,
"未查到相关数据。"
)
count_label.setText(
"统计托数:0"
)
except
sqlite3.DatabaseError as e:
QMessageBox.information(widget,
"提示"
,
"查询为空或数据库错误。"
)
table.clearContents()
table.setRowCount(
0
)
count_label.setText(
"统计托数:0"
)
def
next_page():
global
current_page
current_page
+
=
1
query_data(current_page)
def
prev_page():
global
current_page
if
current_page >
0
:
current_page
-
=
1
query_data(current_page)
app
=
QApplication([])
widget
=
QWidget()
widget.setWindowTitle(
"目的样查询工具"
)
widget.resize(
600
,
400
)
layout
=
QVBoxLayout()
try
:
conn
=
sqlite3.connect(
'C:/Users/TPM/Desktop/VCMDataII.db'
)
cursor
=
conn.cursor()
cursor.execute(
"SELECT DISTINCT BatchId FROM T_BatchInfo"
)
batch_ids
=
[row[
0
]
for
row
in
cursor.fetchall()]
conn.close()
except
sqlite3.DatabaseError as e:
batch_ids
=
[]
option_label1
=
QLabel("")
option_combo1
=
QComboBox()
option_combo1.addItems(batch_ids)
option_hbox1
=
QHBoxLayout()
option_hbox1.addWidget(option_label1)
option_hbox1.addWidget(option_combo1)
layout.addLayout(option_hbox1)
option_label2
=
QLabel(
"选择目的样分类:"
)
option_combo2
=
QComboBox()
option_combo2.addItems([
'包'
,
'P '
,
'接'
,
'开'
,
'停'
])
option_hbox2
=
QHBoxLayout()
option_hbox2.addWidget(option_label2)
option_hbox2.addWidget(option_combo2)
layout.addLayout(option_hbox2)
query_button
=
QPushButton(
"查询"
)
query_button.clicked.connect(
lambda
: query_data(current_page))
layout.addWidget(query_button)
scroll_area
=
QScrollArea()
scroll_area.setWidgetResizable(
True
)
table_widget
=
QWidget()
table_layout
=
QVBoxLayout(table_widget)
table
=
QTableWidget()
table.setColumnCount(
13
)
columns
=
(
'箱'
,
'托'
,
'单'
,
'号'
,
'码'
,
'传'
, 日期
', '
期
', '
上传
', '
虚码
', '
参数一
', '
参数二
', '
目的样类别')
table.setHorizontalHeaderLabels(columns)
for
col
in
range
(
13
):
table.horizontalHeaderItem(col).setTextAlignment(Qt.AlignCenter)
table.setColumnWidth(col,
80
)
table_layout.addWidget(table)
scroll_area.setWidget(table_widget)
layout.addWidget(scroll_area)
count_label
=
QLabel(
"统计托数:0"
)
layout.addWidget(count_label)
next_page_button
=
QPushButton(
"下一页"
)
next_page_button.clicked.connect(next_page)
prev_page_button
=
QPushButton(
"上一页"
)
prev_page_button.clicked.connect(prev_page)
page_hbox
=
QHBoxLayout()
page_hbox.addWidget(prev_page_button)
page_hbox.addWidget(next_page_button)
layout.addLayout(page_hbox)
widget.setLayout(layout)
widget.show()
app.exec_()
import
time