Cont.
Shop 之 查询show
def show_shops():
cursor,db=connect_database()
sql = "SELECT * FROM shop"
results=""
try:
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
location = row[0]
rating = row[1]
shopName = row[2]
id = row[3]
print ("location=%s,rating=%s,shopName=%s,shopID=%s" % \
(location,rating,shopName,id ))
except:
print ("Error: unable to fetch data")
db.close()
return results
Shop 之 添加
def addShop(shopMessage):
cursor,db=connect_database()
shopMessage=shopMessage.split(',')
location=shopMessage[0]
rating=shopMessage[1]
shopName=shopMessage[2]
shopID=shopMessage[3]
sql= f"INSERT INTO shop\
VALUES('{location}','{rating}','{shopName}','{shopID}');"
print(sql)
try:
cursor.execute(sql)
db.commit()
except:
print("insert error")
db.rollback()
return "insert error"
db.close()
return "insert success"
UI Button
class Main(QMainWindow):
def __init__(self):
super(Main, self).__init__()
self.userId = None
# build ui
self.ui = Ui_Order_management_system()
self.ui.setupUi(self)
#show all shops
self.ui.showShops_button.clicked.connect(self.show_shops_button)
#add a shop
self.ui.addShop_button.clicked.connect(self.getShopMessage)
Show shop button
def show_shops_button(self):
#clean this widget
self.ui.shopManagement_table.setRowCount(0)
#set widget colummn as 4
self.ui.shopManagement_table.setColumnCount(4)
self.ui.shopManagement_table.setHorizontalHeaderLabels(['Location','Rating','ShopName','ShopId'])
#Horizontal autofill
self.ui.shopManagement_table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
items = functions.show_shops()
for i in range(len(items)):
item = items[i]
row = self.ui.shopManagement_table.rowCount()
self.ui.shopManagement_table.insertRow(row)
for j in range(len(item)):
item = QTableWidgetItem(str(items[i][j]))
self.ui.shopManagement_table.setItem(row, j, item)
Show shop message
def getShopMessage(self):
text, ok = QInputDialog.getText(self, 'Text Input Dialog', 'input shop message format as (location,rating,shopName,shopID)')
shopMessage=""
if ok and text:
shopMessage=str(text)
message=functions.addShop(shopMessage)
QMessageBox.about(self, "Message", message)
Summary:
- show_shop_button按钮 & 函数
- shop_add_button按钮 & 函数
- show_shop function 函数
- shop_add function 函数
未完待续……