Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. [SOLVED] Problem with QSqlDatabase
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Problem with QSqlDatabase

Scheduled Pinned Locked Moved Mobile and Embedded
27 Posts 6 Posters 29.5k Views 1 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.
  • J Offline
    J Offline
    joonne
    wrote on 18 Dec 2013, 22:18 last edited by
    #14

    I changed the function like you said but the database isn't open so I cant test this but it looks better, thanks.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      panosk
      wrote on 18 Dec 2013, 22:29 last edited by
      #15

      Of course, the error prints when opening the database.... Anyway, I suppose you have also double-checked that the path and database file name are accurate.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 18 Dec 2013, 22:29 last edited by
        #16

        Indeed you should have mention that earlier. Are you running this application on your Windows computer or on the Sailfish emulator/device ? In the second case I don't think it will access your hard drive to get the file hence the error "no such file or directory"

        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
        • S Offline
          S Offline
          stoyanps
          wrote on 18 Dec 2013, 22:50 last edited by
          #17

          First in function OpenDB add this:
          @ if(db_.open()) {
          QStringList tables = db_.tables();
          qDebug() << "Tables: " << tables;
          return true;
          } else {@

          This will show you all tables in your database if connection is successful.

          1. In the beginning of function getUser add this:
            @QSqlDatabase db_ = QSqlDatabase::database();
            if(db_.isOpen()) {
            QSqlQuery query(db_);@
          1 Reply Last reply
          0
          • J Offline
            J Offline
            joonne
            wrote on 19 Dec 2013, 05:42 last edited by
            #18

            Thanks for replies. I am running the application in Sailfish emulator and I think I should now get familiar with the usage of local storage and how to get database available for my Sailfish application. After that I can continue with these comments you have given.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              joonne
              wrote on 19 Dec 2013, 06:50 last edited by
              #19

              I am a bit confused now. Does someone know how I could store my existing database to local storage and how I could refer to it and use it from my c++ class/object? I did found some examples on how to access database from QML but I would like to access my database from the c++ backend.

              1 Reply Last reply
              0
              • P Offline
                P Offline
                p3c0
                Moderators
                wrote on 19 Dec 2013, 07:14 last edited by
                #20

                Hi,

                This test code might help you

                @
                MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
                {
                ui->setupUi(this);

                if(ConnectLocalDB())
                    GetData();
                

                }

                MainWindow::~MainWindow()
                {
                delete ui;
                }

                bool MainWindow::ConnectLocalDB()
                {
                db = QSqlDatabase::addDatabase("QSQLITE");

                QString dbpath = "/root/testd.db";
                db.setDatabaseName(dbpath);
                
                QSqlQuery query(QSqlDatabase::database());
                query.exec&#40;"CREATE TABLE testtable ("
                           "id  INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
                           "testcolumn TEXT&#41;;"
                           );
                
                query.exec&#40;"insert into testtable(testcolumn&#41; values('one')");
                query.exec&#40;"insert into testtable(testcolumn&#41; values('two')");
                query.exec&#40;"insert into testtable(testcolumn&#41; values('three')");
                
                qDebug() << dbpath;
                
                return db.open();
                

                }

                bool MainWindow::GetData()
                {
                QSqlQuery query(QSqlDatabase::database());
                QString q = "select * from testtable;";
                query.exec(q);
                while(query.next())
                {
                qDebug() << query.value(1).toString();
                }
                }
                @

                157

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  joonne
                  wrote on 19 Dec 2013, 10:15 last edited by
                  #21

                  Thanks. I tried with that test code, but I get these errors when I try to open the test database to /root/test.db:

                  @QSqlDatabasePrivate::database: unable to open database: "out of memory Error opening database"
                  QSqlQuery::exec: database not open
                  QSqlQuery::exec: database not open
                  QSqlQuery::exec: database not open
                  QSqlQuery::exec: database not open
                  "/root/testd.db"@

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    p3c0
                    Moderators
                    wrote on 19 Dec 2013, 11:14 last edited by
                    #22

                    Hi,

                    Replace the path of the Database with your path.
                    Which OS are you testing on ?

                    157

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      joonne
                      wrote on 19 Dec 2013, 11:29 last edited by
                      #23

                      I tried to replace it for example with /home/nemo/.local/MyApp which I think is the place where I could store databases with the emulator. I am testing on Sailfish OS emulator on windows 8.

                      I am a bit unsure also about these paths in Qt-Creator, how should I define the path if I would like to access a directory that the emulator also sees and can access?

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        p3c0
                        Moderators
                        wrote on 19 Dec 2013, 12:04 last edited by
                        #24

                        For paths you could use "StandardLocation":http://qt-project.org/doc/qt-5.0/qtcore/qstandardpaths.html#StandardLocation-enum of QStandardPaths.
                        Good to use "QStandardPaths::DataLocation" for data storage.
                        Check manually if the db file was created atleast in that path.

                        157

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          stoyanps
                          wrote on 19 Dec 2013, 13:10 last edited by
                          #25

                          In this test code I think database must be opened before you create and execute QSqlQuery.

                          I am not familiar with Sailfish emulator at all. But you can test whether the problem is only in location of database. Just use the test code from above but for database name use
                          db.setDatabaseName(":memory:");
                          This will create temporary database in memory.

                          1 Reply Last reply
                          0
                          • J Offline
                            J Offline
                            joonne
                            wrote on 7 Jan 2014, 08:25 last edited by
                            #26

                            Thanks for your help, my problem was solved when I learned that I had to access the memory of emulator via SSH connection.

                            1 Reply Last reply
                            0
                            • J Offline
                              J Offline
                              jraichouni
                              wrote on 9 Jan 2014, 07:52 last edited by
                              #27

                              Great, you should
                              mark the thread as [solved] then ;)

                              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