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. Loading data from sqlite database
Forum Update on Tuesday, May 27th 2025

Loading data from sqlite database

Scheduled Pinned Locked Moved Solved General and Desktop
qtcreatorsqlitetableviewbutton
10 Posts 4 Posters 6.7k Views 2 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.
  • K Offline
    K Offline
    Kushan
    wrote on 2 Dec 2017, 07:58 last edited by
    #1

    In my simple Qt c++ application I want to load a set of names from the user table in my sqlite database on to a table view upon a button click. Though the database connection is successfully opened nothing gets loaded to the tableview.

    following is my code

    void MainWindow::on_pushButton_2_clicked()
    {
    mydb = QSqlDatabase::addDatabase("QSQLITE");
    mydb.setDatabaseName("C:/Users/User/Desktop/Qt/Perf.db");
    if(!mydb.open()){
    ui->label->setText("Cant open DB");
    }
    else{
    ui->label->setText("DB successfully opened");
    }
    QSqlQueryModel* modal = new QSqlQueryModel();
    QSqlQuery *qry = new QSqlQuery(mydb);
    qry->prepare("Select * FROM SER_MESAGES");
    qry->exec();
    modal->setQuery(*qry);
    ui->tableView->setModel(modal);

    }

    How can I fix this problem?

    D 1 Reply Last reply 2 Dec 2017, 08:59
    0
    • K Kushan
      2 Dec 2017, 07:58

      In my simple Qt c++ application I want to load a set of names from the user table in my sqlite database on to a table view upon a button click. Though the database connection is successfully opened nothing gets loaded to the tableview.

      following is my code

      void MainWindow::on_pushButton_2_clicked()
      {
      mydb = QSqlDatabase::addDatabase("QSQLITE");
      mydb.setDatabaseName("C:/Users/User/Desktop/Qt/Perf.db");
      if(!mydb.open()){
      ui->label->setText("Cant open DB");
      }
      else{
      ui->label->setText("DB successfully opened");
      }
      QSqlQueryModel* modal = new QSqlQueryModel();
      QSqlQuery *qry = new QSqlQuery(mydb);
      qry->prepare("Select * FROM SER_MESAGES");
      qry->exec();
      modal->setQuery(*qry);
      ui->tableView->setModel(modal);

      }

      How can I fix this problem?

      D Offline
      D Offline
      dream_captain
      wrote on 2 Dec 2017, 08:59 last edited by
      #2

      @Kushan
      Check the results of prepare() and exec()

      bool prepRet = qry->prepare("Select * FROM SER_MESAGES");
      if (!prepRet) {
           qDebug() << qry->lastError().text();
           return;
      }
      if (!qry->exec()) {
           qDebug() << qry->lastError().text();
          return;
      }
      
      K 1 Reply Last reply 2 Dec 2017, 09:13
      1
      • D dream_captain
        2 Dec 2017, 08:59

        @Kushan
        Check the results of prepare() and exec()

        bool prepRet = qry->prepare("Select * FROM SER_MESAGES");
        if (!prepRet) {
             qDebug() << qry->lastError().text();
             return;
        }
        if (!qry->exec()) {
             qDebug() << qry->lastError().text();
            return;
        }
        
        K Offline
        K Offline
        Kushan
        wrote on 2 Dec 2017, 09:13 last edited by
        #3

        @dream_captain I get the message "file is encrypted or is not a database Unable to execute statement""in the console

        D 1 Reply Last reply 2 Dec 2017, 09:24
        0
        • K Kushan
          2 Dec 2017, 09:13

          @dream_captain I get the message "file is encrypted or is not a database Unable to execute statement""in the console

          D Offline
          D Offline
          dream_captain
          wrote on 2 Dec 2017, 09:24 last edited by
          #4

          @Kushan
          Maybe database file is corrupted, or maybe it's a SQLite 2 database. Most likely it's not Qt-related.

          K 1 Reply Last reply 2 Dec 2017, 11:27
          2
          • D dream_captain
            2 Dec 2017, 09:24

            @Kushan
            Maybe database file is corrupted, or maybe it's a SQLite 2 database. Most likely it's not Qt-related.

            K Offline
            K Offline
            Kushan
            wrote on 2 Dec 2017, 11:27 last edited by
            #5

            @dream_captain no Its sqlite3!

            D 1 Reply Last reply 2 Dec 2017, 11:37
            0
            • K Kushan
              2 Dec 2017, 11:27

              @dream_captain no Its sqlite3!

              D Offline
              D Offline
              dream_captain
              wrote on 2 Dec 2017, 11:37 last edited by
              #6

              @Kushan
              Try to open it in database manager (sqlite studio or something like that)

              M 1 Reply Last reply 2 Dec 2017, 15:39
              2
              • D dream_captain
                2 Dec 2017, 11:37

                @Kushan
                Try to open it in database manager (sqlite studio or something like that)

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 2 Dec 2017, 15:39 last edited by
                #7

                Try to open it in database manager (sqlite studio or something like that)

                I can highly recommend this
                http://sqlitebrowser.org/

                1 Reply Last reply
                1
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 2 Dec 2017, 21:00 last edited by
                  #8

                  Hi,

                  What was used to create that database ?

                  Did it use the SEE (SQLite Encryption Extension) ?

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

                  K 1 Reply Last reply 3 Dec 2017, 13:26
                  2
                  • SGaistS SGaist
                    2 Dec 2017, 21:00

                    Hi,

                    What was used to create that database ?

                    Did it use the SEE (SQLite Encryption Extension) ?

                    K Offline
                    K Offline
                    Kushan
                    wrote on 3 Dec 2017, 13:26 last edited by
                    #9

                    @SGaist Thanx got it solved

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 3 Dec 2017, 21:27 last edited by
                      #10

                      What was the problem ?

                      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

                      1/10

                      2 Dec 2017, 07:58

                      • Login

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