Home > database >  How to do AND instead of OR in if statement comparing columns in PyQt5
How to do AND instead of OR in if statement comparing columns in PyQt5

Time:07-28

I have sample data structure like below:

1 2 3 4 5 6 7 8 9 10 11 12 name_x 14 15 16 name_y
CD CD CD CD CD CD CD CD CD CD CD CD Brak CD CD CD Brak
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD Brak

If I use statement as below it means to check if "Brak" string is in column 13 OR column 17. And that is a problem because i cannot execute further functions. How can I rewrite the code below to be used to check if string "Brak" is simultaneously in column 13 and column 17 in any row data.

I am using QTableWidget and Sqlite database.

        self.connection = sqlite3.connect(r'Baza Danych/zestawienie.db')
        self.c = self.connection.cursor()

        query = "SELECT  * FROM Zestawienie"
        result = self.connection.execute(query)

        self.ui.zestawienie_analiza_tab_2.setRowCount(0)
        for row_number, row_data in enumerate(result):
            self.ui.zestawienie_analiza_tab_2.insertRow(row_number)
            for column_number, data in enumerate(row_data):
                self.ui.zestawienie_analiza_tab_2.setItem(row_number, column_number,QTableWidgetItem(str(data)))

                if column_number in(13,17) :

                        if  "Brak"  in data:

                            break
                    
                        else:
                            for i in range(self.ui.zestawienie_analiza_tab_2.columnCount()):
                                for j in range(self.ui.zestawienie_analiza_tab_2.rowCount()):


                                    self.ui.zestawienie_analiza_tab_2.layout = QtWidgets.QHBoxLayout()

                                    img = r"IKONY\letter_k.png"
                                    img2 = r"IKONY\warning.png"

                                    lb = CustomWidget(str(i) str(j), img)
                                    self.ui.zestawienie_analiza_tab_2.layout.addStretch(1)
                                    lb2 = CustomWidget(str(i) str(j), img2)



                                    self.ui.zestawienie_analiza_tab_2.layout.addWidget(lb)
                                    self.ui.zestawienie_analiza_tab_2.layout.addWidget(lb2)

                                    self.ui.zestawienie_analiza_tab_2.cellWidget = QtWidgets.QWidget()
                                    self.ui.zestawienie_analiza_tab_2.cellWidget.setLayout(self.ui.zestawienie_analiza_tab_2.layout)
                                    self.ui.zestawienie_analiza_tab_2.layout.setContentsMargins(0,0,0,0)
                                    self.ui.zestawienie_analiza_tab_2.layout.setSpacing(0)
                                    
                                    self.ui.zestawienie_analiza_tab_2.setCellWidget(row_number, 1,  self.ui.zestawienie_analiza_tab_2.cellWidget)

CodePudding user response:

Do the check when you're looping over rows, not when you loop over columns.

for row_number, row_data in enumerate(result):
    if row_data[13] == 'Brak' and row_data[17] == 'Brak':
        self.ui.zestawienie_analiza_tab_2.insertRow(row_number)
        for column_number, data in enumerate(row_data):
            # rest of your code
  • Related