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. QSqlQuery::value: not positioned on a valid record
Forum Updated to NodeBB v4.3 + New Features

QSqlQuery::value: not positioned on a valid record

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 4 Posters 5.6k Views 3 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.
  • O Offline
    O Offline
    Ocascante
    wrote on last edited by
    #1

    Hello,

    I did a little application using an sqlite database. I added the sqlite database to my project resources files because i need to pack the database into the application

    My code to open the database:
    QSqlDatabase db;
    db = QSqlDatabase::addDatabase("QSQLITE");

    db.setDatabaseName(":/Rutasdb.sqlite");
    if(db.open()){
    qDebug()<<"Database Open!!";
    query.exec("SELECT COUNT(*) FROM table);
    while (query.next(){ .....
    }
    else
    {
    qDebug()<<"NO DATABASE";
    }
    The program opens the database , but generates the error and does not display the data.

    QtCreator 5.5.1 - Ubuntu Linux 14.04

    Thanks for your help!

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      hi and welcome

      Does the exact same code work when you load it from
      hard drive ?

      Its possible to open it in READONLY mode.
      https://www.sqlite.org/c3ref/open.html
      SQLITE_OPEN_READONLY
      maybe you can do that in Qt too ?
      (since as resource, its read only)

      Found it:
      db.setConnectOptions(QLatin1String("QSQLITE_OPEN_READONLY"));

      https://bugreports.qt.io/browse/QTBUG-34920

      O 1 Reply Last reply
      0
      • mrjjM mrjj

        hi and welcome

        Does the exact same code work when you load it from
        hard drive ?

        Its possible to open it in READONLY mode.
        https://www.sqlite.org/c3ref/open.html
        SQLITE_OPEN_READONLY
        maybe you can do that in Qt too ?
        (since as resource, its read only)

        Found it:
        db.setConnectOptions(QLatin1String("QSQLITE_OPEN_READONLY"));

        https://bugreports.qt.io/browse/QTBUG-34920

        O Offline
        O Offline
        Ocascante
        wrote on last edited by
        #3

        Hi,
        Yes, the same code works ok loading from hard disk. I test this code:
        QSqlDatabase db;
        db.setConnectOptions(QLatin1String("QSQLITE_OPEN_READONLY"));
        db = QSqlDatabase::addDatabase("QSQLITE");
        .....

        Got same error

        mrjjM 1 Reply Last reply
        0
        • O Ocascante

          Hi,
          Yes, the same code works ok loading from hard disk. I test this code:
          QSqlDatabase db;
          db.setConnectOptions(QLatin1String("QSQLITE_OPEN_READONLY"));
          db = QSqlDatabase::addDatabase("QSQLITE");
          .....

          Got same error

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Ocascante
          Hmm. Ok.
          So it seems it don't like the db to be in a resource.
          just to be 100% sure can you swap the lines?
          QSqlDatabase db;
          db = QSqlDatabase::addDatabase("QSQLITE");
          db.setConnectOptions(QLatin1String("QSQLITE_OPEN_READONLY"));

          .....

          O 1 Reply Last reply
          0
          • mrjjM mrjj

            @Ocascante
            Hmm. Ok.
            So it seems it don't like the db to be in a resource.
            just to be 100% sure can you swap the lines?
            QSqlDatabase db;
            db = QSqlDatabase::addDatabase("QSQLITE");
            db.setConnectOptions(QLatin1String("QSQLITE_OPEN_READONLY"));

            .....

            O Offline
            O Offline
            Ocascante
            wrote on last edited by Ocascante
            #5

            @mrjj I did it, same error!!

            The code:

            if(db.open()){
            qDebug()<<"DATABASE OPEN";
            query.exec("SELECT COUNT(*) FROM Hours");
            ....

            Debug:

            DATABASE OPEN
            QSqlQuery::exec: database not open

            mrjjM 1 Reply Last reply
            0
            • O Ocascante

              @mrjj I did it, same error!!

              The code:

              if(db.open()){
              qDebug()<<"DATABASE OPEN";
              query.exec("SELECT COUNT(*) FROM Hours");
              ....

              Debug:

              DATABASE OPEN
              QSqlQuery::exec: database not open

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Ocascante
              Ok. so if same code works from hard drive, then it don't like ressources

              https://forum.qt.io/topic/3949/sqlite-database-in-project

              O 1 Reply Last reply
              0
              • mrjjM mrjj

                @Ocascante
                Ok. so if same code works from hard drive, then it don't like ressources

                https://forum.qt.io/topic/3949/sqlite-database-in-project

                O Offline
                O Offline
                Ocascante
                wrote on last edited by
                #7

                @mrjj Do you know any other way to include a database ( not to create ) in a project?
                Thanks again!

                mrjjM 1 Reply Last reply
                0
                • O Ocascante

                  @mrjj Do you know any other way to include a database ( not to create ) in a project?
                  Thanks again!

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @Ocascante
                  well you could have it as resource and then write it out to file.
                  Then use the file.
                  Since you will need tons of DLLS too and such. its it not an option to
                  just include the db file?

                  O 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @Ocascante
                    well you could have it as resource and then write it out to file.
                    Then use the file.
                    Since you will need tons of DLLS too and such. its it not an option to
                    just include the db file?

                    O Offline
                    O Offline
                    Ocascante
                    wrote on last edited by
                    #9

                    @mrjj I did a test including a .csv resource , and then creating the table from this file. The problem is that 1500 records with 5 fields each one, takes more than 5 minutes loading to the database.

                    mrjjM 1 Reply Last reply
                    0
                    • O Ocascante

                      @mrjj I did a test including a .csv resource , and then creating the table from this file. The problem is that 1500 records with 5 fields each one, takes more than 5 minutes loading to the database.

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @Ocascante
                      ok. and the reason you want it in the resource, is to protect it from fiddling ?

                      O 1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Hi and welcome to devnet,

                        As @mrjj suggested, SQLite can't read database from Qt's resources, it's not written for that. However you can copy the database from your executable to the hard drive in one of the writable location provide provided by QStandardPaths

                        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
                        0
                        • mrjjM mrjj

                          @Ocascante
                          ok. and the reason you want it in the resource, is to protect it from fiddling ?

                          O Offline
                          O Offline
                          Ocascante
                          wrote on last edited by
                          #12

                          @mrjj There are two main reasons:

                          I want to build this application for android
                          and to protect it from fiddling.

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            mcosta
                            wrote on last edited by
                            #13

                            Hi,

                            you could embed a database dump and import it each time you start the application

                            Once your problem is solved don't forget to:

                            • Mark the thread as SOLVED using the Topic Tool menu
                            • Vote up the answer(s) that helped you to solve the issue

                            You can embed images using (http://imgur.com/) or (http://postimage.org/)

                            O 1 Reply Last reply
                            0
                            • M mcosta

                              Hi,

                              you could embed a database dump and import it each time you start the application

                              O Offline
                              O Offline
                              Ocascante
                              wrote on last edited by
                              #14

                              @mcosta Hi,

                              I need import a new database file, only when there is an application update.

                              O 1 Reply Last reply
                              0
                              • O Ocascante

                                @mcosta Hi,

                                I need import a new database file, only when there is an application update.

                                O Offline
                                O Offline
                                Ocascante
                                wrote on last edited by
                                #15

                                @Ocascante Hi, thanks for your help. I found the solution using qt resources to pack my sqlite database, the code:

                                QString addin_path =QStandardPaths::writableLocation(QStandardPaths::DataLocation);

                                QDir dir(addin_path);
                                if (!dir.exists())
                                    dir.mkpath(addin_path);
                                if (!dir.exists("addins"))
                                    dir.mkdir("addins");
                                
                                dir.cd("addins");
                                addin_path = dir.absoluteFilePath("Rutasdb.sqlite");
                                qDebug()<<addin_path;
                                
                                if (QFile::exists(addin_path))
                                    QFile::remove(addin_path);
                                
                                QFile::copy(":/Rutasdb.sqlite", addin_path);
                                
                                QSqlDatabase db;
                                db = QSqlDatabase::addDatabase("QSQLITE");
                                db.setConnectOptions(QLatin1String("QSQLITE_OPEN_READONLY"));
                                
                                db.setDatabaseName(addin_path);
                                db.open();
                                

                                Now i can upgrade the data every update.

                                Thanks again.

                                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