Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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)
    

  • Lifetime Qt Champion

    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.


  • Lifetime Qt Champion

    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.



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



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


  • Lifetime Qt Champion

    Which version are you currently using ?



  • @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


  • Lifetime Qt Champion

    How did you install these packages ?



  • @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)


  • Lifetime Qt Champion

    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.



  • @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


  • Lifetime Qt Champion

    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 ?



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


  • Lifetime Qt Champion

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



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


Log in to reply