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. QSqlTableModel not work
Qt 6.11 is out! See what's new in the release blog

QSqlTableModel not work

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 584 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.
  • M Offline
    M Offline
    Mikeeeeee
    wrote on last edited by
    #1

    Hi!
    I connected to SQLite

    {
        ui->setupUi(this);
        myDatabase = QSqlDatabase::addDatabase("QSQLITE");
        myDatabase.setDatabaseName("./sample.db");
        if (myDatabase.open()) {qDebug()<<"db is opened";} else {qDebug()<<"db not opened";}
    
    }
    

    And I'm trying to make a model:

        QSqlTableModel *tableModel = new QSqlTableModel(this);
        tableModel ->setTable("test");
        tableModel->select(); 
        qDebug()<<tableModel->lastError();
    

    But I get error:

    QSqlError("", "Unable to find table test", "")
    

    But I have this table in my database for sure

    JonBJ 1 Reply Last reply
    0
    • M Mikeeeeee

      Hi!
      I connected to SQLite

      {
          ui->setupUi(this);
          myDatabase = QSqlDatabase::addDatabase("QSQLITE");
          myDatabase.setDatabaseName("./sample.db");
          if (myDatabase.open()) {qDebug()<<"db is opened";} else {qDebug()<<"db not opened";}
      
      }
      

      And I'm trying to make a model:

          QSqlTableModel *tableModel = new QSqlTableModel(this);
          tableModel ->setTable("test");
          tableModel->select(); 
          qDebug()<<tableModel->lastError();
      

      But I get error:

      QSqlError("", "Unable to find table test", "")
      

      But I have this table in my database for sure

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

      @Mikeeeeee

      But I have this table in my database for sure

      Well I doubt that "QSqlTableModel not work".

      Why don't you try seeing what tables it sees via: https://doc.qt.io/qt-5/qsqldatabase.html#tables

      1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        One other possible problem is that the default db is not sample.db. try passing the db to the QSqlTableModel explicitly

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        0
        • eyllanescE Offline
          eyllanescE Offline
          eyllanesc
          wrote on last edited by
          #4

          @Mikeeeeee Is the .db in the build folder where the executable is?

          If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Mikeeeeee
            wrote on last edited by
            #5

            Magically, in the morning, everything worked itself out. Sorry for bother.

            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