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. QSqlDatabase.tables() return empty list
Qt 6.11 is out! See what's new in the release blog

QSqlDatabase.tables() return empty list

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 360 Views
  • 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.
  • JM baubetJ Offline
    JM baubetJ Offline
    JM baubet
    wrote on last edited by JM baubet
    #1

    I create 2 tables with QSqlDatabase.query.exec.
    The Table exist but QSqlDatabase.tables() return an empty list. why ?

    from PySide6.QtSql import QSqlDatabase, QSqlQuery, QSql
    
    class MyDb(QSqlDatabase):
        def __init__(self, db_type):
            super().__init__(db_type)
            self.mydb = QSqlDatabase.addDatabase(db_type)
            self.query = QSqlQuery()
    
        def connectDataBase(self, name):
            self.mydb.setDatabaseName(name)
            if not self.mydb.open():
                print("QTableView Example - Error!")
                print("Database Error: {}".format(self.mydb.lastError().databaseText()))
            else:
                print("connexion OK")
    
        def createTableConnexions(self):
            if not self.query.exec(
                    """
                    CREATE TABLE connexions (
                        id INTEGER PRIMARY KEY  UNIQUE NOT NULL,
                        start_time CARACTERE(20) NOT NULL,
                        end_time CARACTERE(20)
                    )
                    """
            ):
                print("query.exec() - Error!")
                print("Database Error: {}".format(self.query.lastError().databaseText()))
            else:
                print("Table Connexions créée ")
    
        def createTableEvenements(self):
            if not self.query.exec(
                """
                CREATE TABLE evenements (
                    id INTEGER PRIMARY KEY  UNIQUE NOT NULL,
                    datetime CARACTERE(20) NOT NULL,
                    event CARACTERE(60)
                )
                """
            ):
                print("query.exec() - Error!")
                print("Database Error: {}".format(self.query.lastError().databaseText()))
            else:
                print("Table Evenements créée ")
    
    
    if __name__ == '__main__':
        mydb = MyDb("QSQLITE")
    
        mydb.connectDataBase("sandBox.sqlite")
        mydb.createTableConnexions()
        mydb.createTableEvenements()
        print(mydb.tables())
    
    

    When I execute the code the fist time :

    connexion OK
    Table Connexions créée 
    Table Evenements créée 
    []
    
    Process finished with exit code 0
    
    

    I execute the code after

    connexion OK
    query.exec() - Error!
    Database Error: table connexions already exists
    query.exec() - Error!
    Database Error: table evenements already exists
    []
    
    Process finished with exit code 0
    
    
    jsulmJ 1 Reply Last reply
    0
    • jsulmJ jsulm

      @JM-baubet Why do you subclass QSqlDatabase?! It makes no sense.
      You're anyway using self.mydb = QSqlDatabase.addDatabase(db_type), so you need to call tables() on self.mydb.

      JM baubetJ Offline
      JM baubetJ Offline
      JM baubet
      wrote on last edited by
      #3

      @jsulm Indeed, it is not necessary to create this class.
      My mind was elsewhere

      Thank you

      1 Reply Last reply
      0
      • JM baubetJ JM baubet

        I create 2 tables with QSqlDatabase.query.exec.
        The Table exist but QSqlDatabase.tables() return an empty list. why ?

        from PySide6.QtSql import QSqlDatabase, QSqlQuery, QSql
        
        class MyDb(QSqlDatabase):
            def __init__(self, db_type):
                super().__init__(db_type)
                self.mydb = QSqlDatabase.addDatabase(db_type)
                self.query = QSqlQuery()
        
            def connectDataBase(self, name):
                self.mydb.setDatabaseName(name)
                if not self.mydb.open():
                    print("QTableView Example - Error!")
                    print("Database Error: {}".format(self.mydb.lastError().databaseText()))
                else:
                    print("connexion OK")
        
            def createTableConnexions(self):
                if not self.query.exec(
                        """
                        CREATE TABLE connexions (
                            id INTEGER PRIMARY KEY  UNIQUE NOT NULL,
                            start_time CARACTERE(20) NOT NULL,
                            end_time CARACTERE(20)
                        )
                        """
                ):
                    print("query.exec() - Error!")
                    print("Database Error: {}".format(self.query.lastError().databaseText()))
                else:
                    print("Table Connexions créée ")
        
            def createTableEvenements(self):
                if not self.query.exec(
                    """
                    CREATE TABLE evenements (
                        id INTEGER PRIMARY KEY  UNIQUE NOT NULL,
                        datetime CARACTERE(20) NOT NULL,
                        event CARACTERE(60)
                    )
                    """
                ):
                    print("query.exec() - Error!")
                    print("Database Error: {}".format(self.query.lastError().databaseText()))
                else:
                    print("Table Evenements créée ")
        
        
        if __name__ == '__main__':
            mydb = MyDb("QSQLITE")
        
            mydb.connectDataBase("sandBox.sqlite")
            mydb.createTableConnexions()
            mydb.createTableEvenements()
            print(mydb.tables())
        
        

        When I execute the code the fist time :

        connexion OK
        Table Connexions créée 
        Table Evenements créée 
        []
        
        Process finished with exit code 0
        
        

        I execute the code after

        connexion OK
        query.exec() - Error!
        Database Error: table connexions already exists
        query.exec() - Error!
        Database Error: table evenements already exists
        []
        
        Process finished with exit code 0
        
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by jsulm
        #2

        @JM-baubet Why do you subclass QSqlDatabase?! It makes no sense.
        You're anyway using self.mydb = QSqlDatabase.addDatabase(db_type), so you need to call tables() on self.mydb.

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

        JM baubetJ 1 Reply Last reply
        2
        • jsulmJ jsulm

          @JM-baubet Why do you subclass QSqlDatabase?! It makes no sense.
          You're anyway using self.mydb = QSqlDatabase.addDatabase(db_type), so you need to call tables() on self.mydb.

          JM baubetJ Offline
          JM baubetJ Offline
          JM baubet
          wrote on last edited by
          #3

          @jsulm Indeed, it is not necessary to create this class.
          My mind was elsewhere

          Thank you

          1 Reply Last reply
          0
          • JM baubetJ JM baubet has marked this topic as solved on

          • Login

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