Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. failing to make the code to connect to a database in PyQt5
Forum Updated to NodeBB v4.3 + New Features

failing to make the code to connect to a database in PyQt5

Scheduled Pinned Locked Moved Solved Qt for Python
14 Posts 3 Posters 1.3k Views 1 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.
  • A Offline
    A Offline
    adrian88888888
    wrote on 9 Sept 2020, 18:13 last edited by
    #1

    1-i'm trying to connect to an already existing database of MySQL, the name of the database is pepe
    2-then i'm trying to connect to a table that already exists, the name is all_my_students

    the problem: the tableView its not showing anything, the gui runs perfectly, there's no error so i can't troubleshoot

    this is the code(dont pay attention to the POO part):

        def Starting_DataBase():
    
            #CONNECTING TO THE DATABASE:
            db = QSqlDatabase.addDatabase("QMYSQL / MARIADB")
            db.setHostName("localhost")
            db.setDatabaseName("pepe")
            db.setUserName("root")
            db.setPassword("123")
            DataBase = .db.open()
    
    
            #MAKING THE MODEL USING THE DATABASE:
            model = QSqlTableModel() #makes the model and speciftes his tipe(theres various tipes of models, this one its for tables of SQL)
            model.setTable("all_my_students")
            model.setEditStrategy(QSqlTableModel.OnManualSubmit)
            model.select()
    
    
            #MAKING THE RELATIONSHIP BETWEEN THE TABLEVIEW AND THE MODEL:
            MyTree.setModel(.model)
    
    J 1 Reply Last reply 9 Sept 2020, 18:23
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 9 Sept 2020, 18:18 last edited by
      #2

      Hi and welcome to devnet,

      Your addDatabase call is invalid.

      If your version of PyQt is recent enough your likely do not have the MySQL backend because it's currently not prebuilt anymore due to a change of licensing from Oracle.

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

      A 1 Reply Last reply 9 Sept 2020, 18:48
      1
      • A adrian88888888
        9 Sept 2020, 18:13

        1-i'm trying to connect to an already existing database of MySQL, the name of the database is pepe
        2-then i'm trying to connect to a table that already exists, the name is all_my_students

        the problem: the tableView its not showing anything, the gui runs perfectly, there's no error so i can't troubleshoot

        this is the code(dont pay attention to the POO part):

            def Starting_DataBase():
        
                #CONNECTING TO THE DATABASE:
                db = QSqlDatabase.addDatabase("QMYSQL / MARIADB")
                db.setHostName("localhost")
                db.setDatabaseName("pepe")
                db.setUserName("root")
                db.setPassword("123")
                DataBase = .db.open()
        
        
                #MAKING THE MODEL USING THE DATABASE:
                model = QSqlTableModel() #makes the model and speciftes his tipe(theres various tipes of models, this one its for tables of SQL)
                model.setTable("all_my_students")
                model.setEditStrategy(QSqlTableModel.OnManualSubmit)
                model.select()
        
        
                #MAKING THE RELATIONSHIP BETWEEN THE TABLEVIEW AND THE MODEL:
                MyTree.setModel(.model)
        
        J Offline
        J Offline
        JonB
        wrote on 9 Sept 2020, 18:23 last edited by
        #3

        @adrian88888888
        Furthermore, you have .db.open() and MyTree.setModel(.model), is that was is actually in your code?

        1 Reply Last reply
        1
        • S SGaist
          9 Sept 2020, 18:18

          Hi and welcome to devnet,

          Your addDatabase call is invalid.

          If your version of PyQt is recent enough your likely do not have the MySQL backend because it's currently not prebuilt anymore due to a change of licensing from Oracle.

          A Offline
          A Offline
          adrian88888888
          wrote on 9 Sept 2020, 18:48 last edited by
          #4

          @SGaist thanks!, what are my options then? should i go to PyQy4?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 9 Sept 2020, 18:50 last edited by
            #5

            Which version are you currently using ?

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

            A 1 Reply Last reply 9 Sept 2020, 18:59
            0
            • S SGaist
              9 Sept 2020, 18:50

              Which version are you currently using ?

              A Offline
              A Offline
              adrian88888888
              wrote on 9 Sept 2020, 18:59 last edited by
              #6

              @SGaist I don't know how to get the exact versions but they are:
              -PySide2
              -PyQt5
              -MySQL 8.0

              .........................................................................................................................................................

              @JonB instead of DataBase = db.open()maybe it is just db.open()?, i put that because of something in the documentation, i suspect that's wrong in some way, in my code there's nothing called DataBase

              but MyTree.setModel(.model) exist in the code, i created the treeView

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 9 Sept 2020, 19:00 last edited by
                #7

                How did you install these packages ?

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

                A 1 Reply Last reply 9 Sept 2020, 19:11
                0
                • S SGaist
                  9 Sept 2020, 19:00

                  How did you install these packages ?

                  A Offline
                  A Offline
                  adrian88888888
                  wrote on 9 Sept 2020, 19:11 last edited by
                  #8

                  @SGaist
                  for being new i can reply every 10 minutes sorry
                  1-pip install PySide2
                  2-I did not install PyQt5(i guess it comes somehow inside pip install PySide2)
                  3-MySQL from the page of the comunity sever: link text

                  in the documentation says that is compatible, in this table link text, it says in the 3rd row: MySQL or MariaDB (version 5.0 and above)

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 9 Sept 2020, 19:55 last edited by
                    #9

                    Then pip can give you the version you installed.

                    PyQt5 and PySide2 are two different projects that are unrelated.

                    What you can do is install a version that uses a version of Qt that is less recent so you'll have the plugin already available.

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

                    A 1 Reply Last reply 9 Sept 2020, 21:03
                    0
                    • S SGaist
                      9 Sept 2020, 19:55

                      Then pip can give you the version you installed.

                      PyQt5 and PySide2 are two different projects that are unrelated.

                      What you can do is install a version that uses a version of Qt that is less recent so you'll have the plugin already available.

                      A Offline
                      A Offline
                      adrian88888888
                      wrote on 9 Sept 2020, 21:03 last edited by adrian88888888 9 Sept 2020, 21:14
                      #10

                      @SGaist the pip showed me that the version of Pyside2 is 5.15.0

                      ohh i remember now, i installed "pip install PyQt5", im newbie so i hardly understand what i'm doing, the version of PyQt5 is 5.13.2

                      so you mean that i should uninstall PyQt5 and put an older version like PyQt4? or i should have an older version of Pyside2?

                      what gets me really lost is that the documentation says that is compatible here, in the 3rd row of the table

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 10 Sept 2020, 19:19 last edited by
                        #11

                        Compatible it is. What is happening is that Oracle changed their licensing therefore the plugin is currently not provided pre-built anymore.

                        So, did you try your code with both libraries ?

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

                        A 1 Reply Last reply 19 Sept 2020, 18:44
                        0
                        • S SGaist
                          10 Sept 2020, 19:19

                          Compatible it is. What is happening is that Oracle changed their licensing therefore the plugin is currently not provided pre-built anymore.

                          So, did you try your code with both libraries ?

                          A Offline
                          A Offline
                          adrian88888888
                          wrote on 19 Sept 2020, 18:44 last edited by
                          #12

                          @SGaist i changed to another database (sqlite) and it worked!, thanks a lot, i wouldn't figured it out on my own

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 19 Sept 2020, 19:30 last edited by
                            #13

                            If you need a remote database system PostgreSQL is also a very good solution.

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

                            A 1 Reply Last reply 19 Sept 2020, 23:28
                            0
                            • S SGaist
                              19 Sept 2020, 19:30

                              If you need a remote database system PostgreSQL is also a very good solution.

                              A Offline
                              A Offline
                              adrian88888888
                              wrote on 19 Sept 2020, 23:28 last edited by
                              #14

                              @SGaist yes i imagine that there's other solutions, but i'm going basic for now

                              1 Reply Last reply
                              0

                              • Login

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