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 Updated to NodeBB v4.3 + New Features

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 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?

    dream_captainD 1 Reply Last reply
    0
    • K Kushan

      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?

      dream_captainD Offline
      dream_captainD Offline
      dream_captain
      wrote on 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
      1
      • dream_captainD dream_captain

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

        dream_captainD 1 Reply Last reply
        0
        • K Kushan

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

          dream_captainD Offline
          dream_captainD Offline
          dream_captain
          wrote on 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
          • dream_captainD dream_captain

            @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 last edited by
            #5

            @dream_captain no Its sqlite3!

            dream_captainD 1 Reply Last reply
            0
            • K Kushan

              @dream_captain no Its sqlite3!

              dream_captainD Offline
              dream_captainD Offline
              dream_captain
              wrote on last edited by
              #6

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

              mrjjM 1 Reply Last reply
              2
              • dream_captainD dream_captain

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

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on 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 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
                  2
                  • SGaistS SGaist

                    Hi,

                    What was used to create that database ?

                    Did it use the SEE (SQLite Encryption Extension) ?

                    K Offline
                    K Offline
                    Kushan
                    wrote on last edited by
                    #9

                    @SGaist Thanx got it solved

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 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

                      • Login

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