Cont. DB Project ------- An Online Retail DB APP

9 篇文章 1 订阅
8 篇文章 1 订阅
该代码段展示了使用Python进行数据库操作,包括查询shopitem表中所有或特定shopId的商品,并在UI上展示。同时,提供了添加新商品到数据库的功能,包含输入验证及事务处理。用户界面设计包含了按钮触发的事件处理函数。
摘要由CSDN通过智能技术生成

Cont.

在这里插入图片描述

Function

item 之 show

def show_shopItems(shopId):
   cursor,db=connect_database()
   sql = f"SELECT * FROM shopitem WHERE ShopId={shopId}"
   if shopId == '' or shopId == 'input shop ID here':
      sql="SELECT * FROM shopitem"
   results=""
   try:

      cursor.execute(sql)
      results = cursor.fetchall()
      for row in results:
         shopId = row[0]
         ItemName = row[1]
         Price = row[2]
         ItemId = row[3]
         Keywords = row[4]
   except:
      print ("Error: unable to fetch data")
      return None
   db.close()
   return results

item 之 add

def add_Item(message):
   message=message.split(',')
   ShopId,ItemName,Price,ItemId,Keywords=message[0:5]
   print(message)
   cursor, db = connect_database()
   sql = f"INSERT INTO shopitem\
            VALUES('{ShopId}','{ItemName}','{Price}','{ItemId}','{Keywords}');"
   print(sql)
   try:
      cursor.execute(sql)
      db.commit()
   except:
      print("insert error")
      db.rollback()
      return "insert error"
   db.close()
   return "insert success"

UI Design

#show items
self.ui.showAllItems_button.clicked.connect(self.show_shopItems)
#add a item
self.ui.addNewItem_button.clicked.connect(self.getItemMessage)

item show

def show_shopItems(self):
        # clean this widget
        self.ui.itemManagement_table.setRowCount(0)
        # set widget colummn as 5
        self.ui.itemManagement_table.setColumnCount(5)
        self.ui.itemManagement_table.setHorizontalHeaderLabels(['ShopId', 'ItemName', 'Price', 'ItemId','Keywords'])
        # Horizontal autofill
        self.ui.itemManagement_table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        items = functions.show_shopItems(self.ui.shopItems_search_text.toPlainText())

        for i in range(len(items)):
            item = items[i]
            row = self.ui.itemManagement_table.rowCount()
            self.ui.itemManagement_table.insertRow(row)
            for j in range(len(item)):
                item = QTableWidgetItem(str(items[i][j]))
                self.ui.itemManagement_table.setItem(row, j, item)

item message

  def getItemMessage(self):
        text, ok = QInputDialog.getText(self, 'Text Input Dialog', 'input item message format as (ShopId,ItemName,Price,ItemId,keywords)')
        itemMessage=""
        print(text)
        if ok and text:
            itemMessage=str(text)
        message=functions.add_Item(itemMessage)
        QMessageBox.about(self, "Message", message)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cmy_CTO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值