首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更有效地从qtableWidget更新和插入MYSQL数据库

问更有效地从qtableWidget更新和插入MYSQL数据库
EN

Stack Overflow用户
提问于 2021-12-29 09:29:02
回答 1查看 549关注 0票数 0

我正在构建一个带有PyQt5的桌面应用程序来连接、加载数据、插入数据和更新MySQL数据库。我想出的更新数据库和将数据插入数据库的方法是可行的。但是我觉得在计算速度上应该有一个更快的方法。如果有人能帮忙那会很有帮助的。我现在要更新数据库的是-

代码语言:javascript
复制
def log_change(self, item):
        self.changed_items.append([item.row(),item.column()])
        
        # I connect this function to the item changed signal to log any cells which have been changed 


def update_db(self):
        
        # Creating an empty list to remove the duplicated cells from the initial list
        
        self.changed_items_load= []
        [self.changed_items_load.append(x) for x in self.changed_items if x not in self.changed_items_load]
        
        # loop through the changed_items list and remove cells with no values in them
        
        for db_wa in self.changed_items_load:
            if self.tableWidget.item(db_wa[0],db_wa[1]).text() == "":
                self.changed_items_load.remove(db_wa)
        
        try:
            
            mycursor = mydb.cursor()
            
            # loop through the list and update the database cell by cell
            
            for ecr in self.changed_items_load:
                    
                    command = ("update table1 set `{col_name}` = %s where id=%s;")
                    
                    # table widget column name matches db table column name
                    
                    data = (str(self.tableWidget.item(ecr[0],ecr[1]).text()),int(self.tableWidget.item(ecr[0],0).text()))
                    
                    mycursor.execute(command.format(col_name = self.col_names[ecr[1]]),data)
                
                    # self.col_names is a list of the tableWidget columns 
                    
            mydb.commit()        
            mycursor.close()
            
        except OperationalError:
            Msgbox = QMessageBox()
            Msgbox.setText("Error! Connection to database lost!")
            Msgbox.exec()
        
        except NameError:
            Msgbox = QMessageBox()
            Msgbox.setText("Error! Connect to database!")
            Msgbox.exec()

为了将数据和新行插入到db中,我可以在网上找到有关这方面的一些信息。但是,我无法一次插入多行,也无法为每一行插入不同的列长。就像我只想在第1行插入2列,然后在第2行插入3列.差不多是这样的。

代码语言:javascript
复制
def insert_db(self):
        
        # creating a list of each column
        
        self.a = [self.tableWidget.item(row,1).text() for row in range (self.tableWidget.rowCount()) if self.tableWidget.item(row,1) != None]
        self.b = [self.tableWidget.item(row,2).text() for row in range (self.tableWidget.rowCount()) if self.tableWidget.item(row,2) != None]
        self.c =  [self.tableWidget.item(row,3).text() for row in range (self.tableWidget.rowCount()) if self.tableWidget.item(row,3) != None]
        self.d =  [self.tableWidget.item(row,4).text() for row in range (self.tableWidget.rowCount()) if self.tableWidget.item(row,4) != None]
        
        
        try:
            
            mycursor = mydb.cursor()
            
            mycursor.execute("INSERT INTO table1(Name, Date, Quantity, Comments) VALUES ('%s', '%s', '%s', '%s')" %(''.join(self.a),
                                                                ''.join(self.b),
                                                                ''.join(self.c),
                                                                ''.join(self.d)))
                
                
                    
            mydb.commit()        
            mycursor.close()
            
        except OperationalError:
            Msgbox = QMessageBox()
            Msgbox.setText("Error! Connection to database lost!")
            Msgbox.exec()
        
        except NameError:
            Msgbox = QMessageBox()
            Msgbox.setText("Error! Connect to database!")
            Msgbox.exec()

我会感谢你的帮助。谢谢。

EN

回答 1

Stack Overflow用户

发布于 2021-12-30 19:05:44

,如果我想在第1行插入2列,然后在第2行插入3列,则类似于

不是的。给定的数据库表有特定数量的列。这是“表格”定义的一个组成部分。

INSERT向表中添加新行。可以构造一个“一次性”插入多行的单一SQL语句。

UPDATE修改表的一行或多行。行由Update语句中指定的某些条件指示。

用%s构造SQL是有风险的--如果插入字符串中有引号,就会遇到麻烦。

(我希望这些评论能帮助你进入下一个理解数据库的阶段。)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70517509

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档