Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • 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?
Forum Updated to NodeBB v4.3 + New Features

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 4.1k 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.
  • Kent-DorfmanK Offline
    Kent-DorfmanK Offline
    Kent-Dorfman
    wrote on last edited by
    #2

    being that this is python (indentation specific scope) please repost your code using the "</>" code block to preserve the indentation and make it readable.

    A 1 Reply Last reply
    2
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #3

      Hi and welcome to devnet,

      What about using a QSqlQueryModel ?

      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
      1
      • Kent-DorfmanK Kent-Dorfman

        being that this is python (indentation specific scope) please repost your code using the "</>" code block to preserve the indentation and make it readable.

        A Offline
        A Offline
        Abhishek_98
        wrote on last edited by
        #4

        @Kent-Dorfman Done it, please take a look

        JonBJ 1 Reply Last reply
        0
        • A Abhishek_98

          @Kent-Dorfman Done it, please take a look

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

          @Abhishek_98
          Before you go any further, as @SGaist has suggested you should first decide whether you wish to use Qt's SQL classes rather than sticking with your Python ones. Qt's model classes (e.g. QSqlQueryModel) play directly with Qt's view classes (QTableView, QListView). Do you have a reason for desiring the Python ones, which will require more work to interoperate with the UI?

          A 1 Reply Last reply
          0
          • JonBJ JonB

            @Abhishek_98
            Before you go any further, as @SGaist has suggested you should first decide whether you wish to use Qt's SQL classes rather than sticking with your Python ones. Qt's model classes (e.g. QSqlQueryModel) play directly with Qt's view classes (QTableView, QListView). Do you have a reason for desiring the Python ones, which will require more work to interoperate with the UI?

            A Offline
            A Offline
            Abhishek_98
            wrote on last edited by
            #6

            @SGaist I prefer python over Qt's SQL classes, as I am currently undergoing my python training classes and moreover I am much more comfortable with python rather than Qt's exclusive SQL language.

            jsulmJ 1 Reply Last reply
            0
            • A Abhishek_98

              @SGaist I prefer python over Qt's SQL classes, as I am currently undergoing my python training classes and moreover I am much more comfortable with python rather than Qt's exclusive SQL language.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by jsulm
              #7

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

              Qt's exclusive SQL language

              there is no such thing. Qt simply provides APIs to access SQL databases, but SQL syntax is same and depends on used SQL database, not Qt.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Abhishek_98
                wrote on last edited by
                #8

                can anyone please just tell my why the items aren't being displayed in the listwidget? Is there something wrong with the code?

                jsulmJ JonBJ 2 Replies Last reply
                0
                • A Abhishek_98

                  can anyone please just tell my why the items aren't being displayed in the listwidget? Is there something wrong with the code?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by jsulm
                  #9

                  @Abhishek_98 Did you check the length of rec?
                  "The records get fetched (I've tried fetching on cmd and it works)" - it may work in cmd but not in your app.
                  Best would be to debug to see what happens.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1
                  • A Abhishek_98

                    can anyone please just tell my why the items aren't being displayed in the listwidget? Is there something wrong with the code?

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

                    @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
                    2
                    • Kent-DorfmanK Offline
                      Kent-DorfmanK Offline
                      Kent-Dorfman
                      wrote on last edited by Kent-Dorfman
                      #11

                      two comments:

                      1. why do you do cur=db.cursor() twice?

                      2. your problem may be related to the format that the data is returned in rec (assuming rec actually has something in it). What does the print(rec) actually print out?

                      3)skip what I wrote above...as JonB ppointed out... cur.fetchall() will return a LIST of records, and within each record will be a LIST of fields. You are not parsing your SQL response properly.

                      1 Reply Last reply
                      2
                      • 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

                                          • Login

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