Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do I display the records fetched from my database to my listwidget?
Qt 6.12 LTS is out! See what's new in the release blog

How do I display the records fetched from my database to my listwidget?

Scheduled Pinned Locked Moved Unsolved General and Desktop
25 Posts 7 Posters 8.6k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tejaswini_14
    wrote on last edited by
    #12

    I have the same problem...Please help if you have the solution

    SGaistS 1 Reply Last reply
    0
    • T Tejaswini_14

      I have the same problem...Please help if you have the solution

      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #13

      @Tejaswini_14 hi and welcome to devnet,

      Did you follow what people already suggested/asked to do on this thread ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • T Offline
        T Offline
        Tejaswini_14
        wrote on last edited by
        #14

        Actually I am at the beginning level in Python and I have made some changes to my code but whenever I click on QRadioButton it shows "Python has stopped working".

        JonBJ 1 Reply Last reply
        0
        • T Tejaswini_14

          Actually I am at the beginning level in Python and I have made some changes to my code but whenever I click on QRadioButton it shows "Python has stopped working".

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #15

          @Tejaswini_14
          Sorry, but nobody is going to be able to help you from that description of a problem.

          1 Reply Last reply
          0
          • T Offline
            T Offline
            Tejaswini_14
            wrote on last edited by
            #16

            Here is my code

            from PyQt5 import QtCore, QtGui, QtWidgets
            import sqlite3
            db=sqlite3.connect("fantcktdb.db")
            cur=db.cursor()
            
            class Ui_MainWindow(object):
                def setupUi(self, MainWindow):
                    MainWindow.setObjectName("MainWindow")
                    MainWindow.resize(800, 600)
                    self.centralwidget = QtWidgets.QWidget(MainWindow)
                    self.centralwidget.setObjectName("centralwidget")
                    self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
                    self.gridLayout.setObjectName("gridLayout")
                    self.frame = QtWidgets.QFrame(self.centralwidget)
                    self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
                    self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
                    self.frame.setObjectName("frame")
                    self.gridLayout_2 = QtWidgets.QGridLayout(self.frame)
                    self.gridLayout_2.setObjectName("gridLayout_2")
                    self.verticalLayout = QtWidgets.QVBoxLayout()
                    self.verticalLayout.setObjectName("verticalLayout")
                    self.wkRadio = QtWidgets.QRadioButton(self.frame)
                    self.wkRadio.setObjectName("wkRadio")
                    self.buttonGroup = QtWidgets.QButtonGroup(MainWindow)
                    self.buttonGroup.setObjectName("buttonGroup")
                    self.buttonGroup.addButton(self.wkRadio)
                    self.verticalLayout.addWidget(self.wkRadio)
                    self.arRadio = QtWidgets.QRadioButton(self.frame)
                    self.arRadio.setObjectName("arRadio")
                    self.buttonGroup.addButton(self.arRadio)
                    self.verticalLayout.addWidget(self.arRadio)
                    self.bowRadio = QtWidgets.QRadioButton(self.frame)
                    self.bowRadio.setObjectName("bowRadio")
                    self.buttonGroup.addButton(self.bowRadio)
                    self.verticalLayout.addWidget(self.bowRadio)
                    self.batRadio = QtWidgets.QRadioButton(self.frame)
                    self.batRadio.setObjectName("batRadio")
                    self.buttonGroup.addButton(self.batRadio)
                    self.verticalLayout.addWidget(self.batRadio)
                    self.buttonGroup.buttonToggled.connect(self.showList)
                    self.listWidget = QtWidgets.QListWidget(self.frame)
                    self.listWidget.setObjectName("listWidget")
                    self.verticalLayout.addWidget(self.listWidget)
                    self.gridLayout_2.addLayout(self.verticalLayout, 0, 0, 1, 1)
                    self.gridLayout.addWidget(self.frame, 0, 0, 1, 1)
                    MainWindow.setCentralWidget(self.centralwidget)
                    self.menubar = QtWidgets.QMenuBar(MainWindow)
                    self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
                    self.menubar.setObjectName("menubar")
                    MainWindow.setMenuBar(self.menubar)
                    self.statusbar = QtWidgets.QStatusBar(MainWindow)
                    self.statusbar.setObjectName("statusbar")
                    MainWindow.setStatusBar(self.statusbar)
            
                    self.retranslateUi(MainWindow)
                    QtCore.QMetaObject.connectSlotsByName(MainWindow)
            
                def showList(self,MainWindow):
                    if (self.batRadio.isChecked()==True):
                        cur.execute('''SELECT Player FROM stats WHERE Ctg='BAT';''')
                        records=cur.fetchall()
                        self.listWidget.clear()
                        for row in records:
                            self.listWidget.addItems(row)
                        self.listWidget.update()
                    elif (self.bowRadio.isChecked()==True):
                        cur.execute('''SELECT Player FROM stats WHERE Ctg='BOW';''')
                        records=cur.fetchall()
                        self.listWidget.clear()
                        for row in records:
                            self.listWidget.addItems(row)
                        self.listWidget.update()
                    elif (self.arRadio.isChecked()==True):
                        cur.execute('''SELECT Player FROM stats WHERE Ctg='AR';''')
                        records=cur.fetchall()
                        self.listWidget.clear()
                        for row in records:
                            self.listWidget.addItems(row)
                        self.listWidget.update()
                    elif (self.wkRadio.isChecked()==True):
                        cur.execute('''SELECT Player FROM stats WHERE Ctg='WK';''')
                        records=cur.fetchall()
                        self.listWidget.clear()
                        for row in records:
                            self.listWidget.addItems(row)
                        self.listWidget.update()
                def retranslateUi(self, MainWindow):
                    _translate = QtCore.QCoreApplication.translate
                    MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
                    self.wkRadio.setText(_translate("MainWindow", "WK"))
                    self.arRadio.setText(_translate("MainWindow", "AR"))
                    self.bowRadio.setText(_translate("MainWindow", "BOW"))
                    self.batRadio.setText(_translate("MainWindow", "BAT"))
            
            
            if __name__ == "__main__":
                import sys
                app = QtWidgets.QApplication(sys.argv)
                MainWindow = QtWidgets.QMainWindow()
                ui = Ui_MainWindow()
                ui.setupUi(MainWindow)
                MainWindow.show()
                sys.exit(app.exec_())
            
            JonBJ 1 Reply Last reply
            0
            • T Tejaswini_14

              Here is my code

              from PyQt5 import QtCore, QtGui, QtWidgets
              import sqlite3
              db=sqlite3.connect("fantcktdb.db")
              cur=db.cursor()
              
              class Ui_MainWindow(object):
                  def setupUi(self, MainWindow):
                      MainWindow.setObjectName("MainWindow")
                      MainWindow.resize(800, 600)
                      self.centralwidget = QtWidgets.QWidget(MainWindow)
                      self.centralwidget.setObjectName("centralwidget")
                      self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
                      self.gridLayout.setObjectName("gridLayout")
                      self.frame = QtWidgets.QFrame(self.centralwidget)
                      self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
                      self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
                      self.frame.setObjectName("frame")
                      self.gridLayout_2 = QtWidgets.QGridLayout(self.frame)
                      self.gridLayout_2.setObjectName("gridLayout_2")
                      self.verticalLayout = QtWidgets.QVBoxLayout()
                      self.verticalLayout.setObjectName("verticalLayout")
                      self.wkRadio = QtWidgets.QRadioButton(self.frame)
                      self.wkRadio.setObjectName("wkRadio")
                      self.buttonGroup = QtWidgets.QButtonGroup(MainWindow)
                      self.buttonGroup.setObjectName("buttonGroup")
                      self.buttonGroup.addButton(self.wkRadio)
                      self.verticalLayout.addWidget(self.wkRadio)
                      self.arRadio = QtWidgets.QRadioButton(self.frame)
                      self.arRadio.setObjectName("arRadio")
                      self.buttonGroup.addButton(self.arRadio)
                      self.verticalLayout.addWidget(self.arRadio)
                      self.bowRadio = QtWidgets.QRadioButton(self.frame)
                      self.bowRadio.setObjectName("bowRadio")
                      self.buttonGroup.addButton(self.bowRadio)
                      self.verticalLayout.addWidget(self.bowRadio)
                      self.batRadio = QtWidgets.QRadioButton(self.frame)
                      self.batRadio.setObjectName("batRadio")
                      self.buttonGroup.addButton(self.batRadio)
                      self.verticalLayout.addWidget(self.batRadio)
                      self.buttonGroup.buttonToggled.connect(self.showList)
                      self.listWidget = QtWidgets.QListWidget(self.frame)
                      self.listWidget.setObjectName("listWidget")
                      self.verticalLayout.addWidget(self.listWidget)
                      self.gridLayout_2.addLayout(self.verticalLayout, 0, 0, 1, 1)
                      self.gridLayout.addWidget(self.frame, 0, 0, 1, 1)
                      MainWindow.setCentralWidget(self.centralwidget)
                      self.menubar = QtWidgets.QMenuBar(MainWindow)
                      self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
                      self.menubar.setObjectName("menubar")
                      MainWindow.setMenuBar(self.menubar)
                      self.statusbar = QtWidgets.QStatusBar(MainWindow)
                      self.statusbar.setObjectName("statusbar")
                      MainWindow.setStatusBar(self.statusbar)
              
                      self.retranslateUi(MainWindow)
                      QtCore.QMetaObject.connectSlotsByName(MainWindow)
              
                  def showList(self,MainWindow):
                      if (self.batRadio.isChecked()==True):
                          cur.execute('''SELECT Player FROM stats WHERE Ctg='BAT';''')
                          records=cur.fetchall()
                          self.listWidget.clear()
                          for row in records:
                              self.listWidget.addItems(row)
                          self.listWidget.update()
                      elif (self.bowRadio.isChecked()==True):
                          cur.execute('''SELECT Player FROM stats WHERE Ctg='BOW';''')
                          records=cur.fetchall()
                          self.listWidget.clear()
                          for row in records:
                              self.listWidget.addItems(row)
                          self.listWidget.update()
                      elif (self.arRadio.isChecked()==True):
                          cur.execute('''SELECT Player FROM stats WHERE Ctg='AR';''')
                          records=cur.fetchall()
                          self.listWidget.clear()
                          for row in records:
                              self.listWidget.addItems(row)
                          self.listWidget.update()
                      elif (self.wkRadio.isChecked()==True):
                          cur.execute('''SELECT Player FROM stats WHERE Ctg='WK';''')
                          records=cur.fetchall()
                          self.listWidget.clear()
                          for row in records:
                              self.listWidget.addItems(row)
                          self.listWidget.update()
                  def retranslateUi(self, MainWindow):
                      _translate = QtCore.QCoreApplication.translate
                      MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
                      self.wkRadio.setText(_translate("MainWindow", "WK"))
                      self.arRadio.setText(_translate("MainWindow", "AR"))
                      self.bowRadio.setText(_translate("MainWindow", "BOW"))
                      self.batRadio.setText(_translate("MainWindow", "BAT"))
              
              
              if __name__ == "__main__":
                  import sys
                  app = QtWidgets.QApplication(sys.argv)
                  MainWindow = QtWidgets.QMainWindow()
                  ui = Ui_MainWindow()
                  ui.setupUi(MainWindow)
                  MainWindow.show()
                  sys.exit(app.exec_())
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #17

              @Tejaswini_14
              I think you need to read carefully through the posts above. If I say so myself, you/the OP did not respond to my own reply, which I think would indicate you cannot just go for row in records::

              @Abhishek_98
              Do you not get items added to the list widget, or do you get blank items? What is item.text() each time?

              rec=cur.fetchall()

              Does that not return a list of rows?

              for item in rec:

              Then item would be a row, not a column value in a row. Even if it's a result set with just one row of one column. If item is a row, what is item.text()?

              Python's print(rec) can print what it likes, probably str(rec).

              1 Reply Last reply
              1
              • T Offline
                T Offline
                Tejaswini_14
                wrote on last edited by
                #18

                I am fetching only one column which is "Player", then why should I use index values. Also I have tried to use index when I fetch all columns. e.g.

                if (self.batRadio.isChecked()==True):
                            bat='BAT'
                            sql="SELECT * FROM stats WHERE Ctg='"+bat+"';"
                            cur.execute(sql)
                            records=cur.fetchall()
                            for row in records:
                                item=row[0]
                                print(item)
                                self.listWidget.addItem(item.text())
                
                JonBJ 1 Reply Last reply
                0
                • T Tejaswini_14

                  I am fetching only one column which is "Player", then why should I use index values. Also I have tried to use index when I fetch all columns. e.g.

                  if (self.batRadio.isChecked()==True):
                              bat='BAT'
                              sql="SELECT * FROM stats WHERE Ctg='"+bat+"';"
                              cur.execute(sql)
                              records=cur.fetchall()
                              for row in records:
                                  item=row[0]
                                  print(item)
                                  self.listWidget.addItem(item.text())
                  
                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #19

                  @Tejaswini_14 said in How do I display the records fetched from my database to my listwidget?:

                  I am fetching only one column which is "Player", then why should I use index values

                  Because the fact that your particular SELECT statement is only returning one column, instead of say SELECT column1, column2, column3, does not alter cur.fetchall()'s behaviour of returning a list or rows, each of which holds a list of columns. Hence I assume you need row[0] to pick out column #0.

                  Also I have tried to use index when I fetch all columns. e.g.

                  And? You don't say what happens? You have print(item) yet you don't bother to tell us what that shows, or even if it gets hit? And I asked

                  What is item.text() each time?

                  but you don't show that, so I don't know what you actually passing in self.listWidget.addItem(item.text())?

                  1 Reply Last reply
                  1
                  • T Offline
                    T Offline
                    Tejaswini_14
                    wrote on last edited by
                    #20

                    I am getting error like this when I execute query seperately

                    Traceback (most recent call last):
                    File "C:\Python36\Scripts\FantasyCricket.py", line 17, in <module>
                    cur.execute('''SELECT * FROM stats WHERE Ctg="BAT";''')
                    sqlite3.OperationalError: no such table: stats
                    

                    Even if I have the table stats stored in database

                    mrjjM JonBJ 2 Replies Last reply
                    0
                    • T Tejaswini_14

                      I am getting error like this when I execute query seperately

                      Traceback (most recent call last):
                      File "C:\Python36\Scripts\FantasyCricket.py", line 17, in <module>
                      cur.execute('''SELECT * FROM stats WHERE Ctg="BAT";''')
                      sqlite3.OperationalError: no such table: stats
                      

                      Even if I have the table stats stored in database

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #21

                      @Tejaswini_14

                      Hi
                      make very, very sure that it finds
                      "fantcktdb.db"
                      else it will create new empty one
                      so if you have yours in say your project folder, then
                      when run it might create a new one in build folder.
                      so please use full path to it to be sure it find the one you have data in.

                      T 1 Reply Last reply
                      3
                      • T Tejaswini_14

                        I am getting error like this when I execute query seperately

                        Traceback (most recent call last):
                        File "C:\Python36\Scripts\FantasyCricket.py", line 17, in <module>
                        cur.execute('''SELECT * FROM stats WHERE Ctg="BAT";''')
                        sqlite3.OperationalError: no such table: stats
                        

                        Even if I have the table stats stored in database

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #22

                        @Tejaswini_14
                        So since you are using Python sqlite3 library calls, not Qt ones, to access your database this is a Python issue not a Qt one, and you would be better asking that in a Python/SQLite forum. It also means that adding to a QListWidget is not itself an issue.

                        EDIT This post has crossed with @mrjj's which is quite possibly the cause. If you're not sure where the database file is being picked up, perhaps (temporarily) change your db=sqlite3.connect("fantcktdb.db") to specify the full path to the intended file location.

                        T 1 Reply Last reply
                        3
                        • mrjjM mrjj

                          @Tejaswini_14

                          Hi
                          make very, very sure that it finds
                          "fantcktdb.db"
                          else it will create new empty one
                          so if you have yours in say your project folder, then
                          when run it might create a new one in build folder.
                          so please use full path to it to be sure it find the one you have data in.

                          T Offline
                          T Offline
                          Tejaswini_14
                          wrote on last edited by
                          #23

                          @mrjj It helped me. Thanks a lot!! But I have provided the path as- "C:\Python36\fantcktdb.db"
                          and also I needed to open and close the database connection for every QRadioButton

                          JonBJ 1 Reply Last reply
                          0
                          • JonBJ JonB

                            @Tejaswini_14
                            So since you are using Python sqlite3 library calls, not Qt ones, to access your database this is a Python issue not a Qt one, and you would be better asking that in a Python/SQLite forum. It also means that adding to a QListWidget is not itself an issue.

                            EDIT This post has crossed with @mrjj's which is quite possibly the cause. If you're not sure where the database file is being picked up, perhaps (temporarily) change your db=sqlite3.connect("fantcktdb.db") to specify the full path to the intended file location.

                            T Offline
                            T Offline
                            Tejaswini_14
                            wrote on last edited by
                            #24

                            @JonB Thanks a lot!! It worked for me.

                            1 Reply Last reply
                            0
                            • T Tejaswini_14

                              @mrjj It helped me. Thanks a lot!! But I have provided the path as- "C:\Python36\fantcktdb.db"
                              and also I needed to open and close the database connection for every QRadioButton

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by
                              #25

                              @Tejaswini_14 said in How do I display the records fetched from my database to my listwidget?:

                              But I have provided the path as- "C:\Python36\fantcktdb.db"

                              Be careful how you do this in Python. If you have a literal you must double the backslahes, e.g.

                              db=sqlite3.connect("C:\\Python36\\fantcktdb.db")
                              

                              and also I needed to open and close the database connection for every QRadioButton

                              You should not be doing this. Opening/closing a db is "slow", you should keep a connection open and re-use it.

                              1 Reply Last reply
                              1

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt
                              • Unsolved