Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Loading Sqlite database within the project directory
Forum Update on Tuesday, May 27th 2025

Loading Sqlite database within the project directory

Scheduled Pinned Locked Moved QML and Qt Quick
13 Posts 5 Posters 17.4k 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.
  • R Offline
    R Offline
    raghava.chinnappa
    wrote on 21 Jan 2011, 18:55 last edited by
    #1

    I followed the thread "http://developer.qt.nokia.com/forums/viewthread/2093":http://developer.qt.nokia.com/forums/viewthread/2093 and created the following files which is inside the project directory

    @1. Qt.md5(“mydb_name”).ini
    2. Qt.md5(“mydb_name”).sqlite
    3. Qt.md5(“mydb_name”) folder@

    Still pointing to the default database in window7 path "C:\Users<username>\AppData\Local\Nokia\QtSimulator\data\QML\OfflineStorage"

    Even I tried to change the default path to custom path in main.cpp

    @ QDeclarativeEngine engine;
    QDir dir;
    QString customPath = "qml/Sqlite-Example";

    if(dir.mkpath(QString(customPath))){
        engine.setOfflineStoragePath(QString(customPath));
    }
    
    QString str = engine.offlineStoragePath();
    qDebug() << str;
    

    @

    It doesn't help me. Any thoghts on this?

    1 Reply Last reply
    0
    • B Offline
      B Offline
      blam
      wrote on 24 Jan 2011, 23:18 last edited by
      #2

      The files need to be within a "Databases" subdirectory.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        raghava.chinnappa
        wrote on 24 Jan 2011, 23:24 last edited by
        #3

        I tried that option also. Didn't worked!

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mbrasser
          wrote on 25 Jan 2011, 00:00 last edited by
          #4

          Hi,

          There may be a bug with how path separators are handled by the engine. Does it work if you change from / to \? (you'll still need the Databases subdirectory that blam mentioned as well)

          Regards,
          Michael

          1 Reply Last reply
          0
          • R Offline
            R Offline
            raghava.chinnappa
            wrote on 25 Jan 2011, 00:10 last edited by
            #5

            It seems to a bug in QDeclarativeEngine.setOfflineStoragePath() function. I have tried all the possible ways.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mbrasser
              wrote on 25 Jan 2011, 00:23 last edited by
              #6

              Can you give more details on what you tried and the results you got? (e.g. is your debug outputting the correct string? What failure message are you getting?)

              1 Reply Last reply
              0
              • R Offline
                R Offline
                raghava.chinnappa
                wrote on 25 Jan 2011, 00:50 last edited by
                #7

                The Project folder structure is like this
                @+ sqlite
                \Databases
                \0cbc6611f5540bd0809a388dc95a615b
                \0cbc6611f5540bd0809a388dc95a615b.ini
                \0cbc6611f5540bd0809a388dc95a615b.sql
                \main.qml
                @

                Source code for main.cpp
                @#include <QtGui/QApplication>
                #include "qmlapplicationviewer.h"
                #include <QDebug>
                #include <QDeclarativeEngine>
                #include <QDir>
                #include <QString>

                int main(int argc, char *argv[])
                {
                QApplication app(argc, argv);

                QDeclarativeEngine engine;
                QString customPath = "qml/Sqlite";
                
                QDir dir;
                if(dir.mkpath(QString(customPath))){
                    qDebug() << "Default path >> "+engine.offlineStoragePath();
                    engine.setOfflineStoragePath(QString(customPath));
                    qDebug() << "New path >> "+engine.offlineStoragePath();
                }
                
                QmlApplicationViewer viewer;
                viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
                viewer.setMainQmlFile&#40;QLatin1String("qml/Sqlite/main.qml"&#41;);
                viewer.showExpanded();
                
                return app.exec();
                

                }
                @

                source code for main.qml is

                @import Qt 4.7

                Rectangle {
                color: "#ffffff"
                width: 200
                height: 100

                 Text {
                     id: textArea
                     text: "?"
                     anchors.horizontalCenter: parent.horizontalCenter
                     Component.onCompleted: findGreetings()
                 }
                
                 function findGreetings() {
                     var db = openDatabaseSync("Test", "1.0", "The Example QML SQL!", 1000000);
                     db.transaction(
                         function(tx) {
                
                             // Show all added greetings
                             var rs = tx.executeSql('SELECT * FROM Greeting');
                
                             var r = ""
                             for(var i = 0; i < rs.rows.length; i++) {
                                 r += rs.rows.item(i).salutation + ", " + rs.rows.item(i).salutee + "\n";
                             }
                             print(r);
                             textArea.text = r;
                         }
                     )
                 }
                

                }
                @

                And finally I have 3 entries in the 0cbc6611f5540bd0809a388dc95a615b.sql file which stored at the default Database storage path C:\Users<username>\AppData\Local\Nokia\QtSimulator\data\QML\OfflineStorage\Databases

                @"Mr.", "Apple"
                "Mr.", "Ball"
                "Mr.", "Cat"@

                And I have ZERO entries in the 0cbc6611f5540bd0809a388dc95a615b.sql file, which stored inside my project folder like "qml/Sqlite/Databases".

                Technically, I should get no results at "Application Output" in the Qt-Creator & no text update on the QML Text component. But Its always updated with 3 entries of rows from the default location.

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  jpetrell
                  wrote on 25 Jan 2011, 01:58 last edited by
                  #8

                  Qt supports creating multiple declarative engines that can be used to execute different QML files. You have two QDeclarativeEngine's in your example code, your own engine which you set the custom off-line storage path, and a separate engine inside QmlApplicationViewer that loads the "qml/Sqlite/main.qml", which has no knowledge of the new custom path set to the another engine.

                  The off-line storage path will come into effect when you call
                  @QmlApplicationViewer viewer;
                  viewer.engine()->setOfflineStoragePath(QString(customPath));@

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    raghava.chinnappa
                    wrote on 25 Jan 2011, 02:16 last edited by
                    #9

                    Wow, It worked finally. Thanks lot @jpetrell

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      raghava.chinnappa
                      wrote on 26 Jan 2011, 22:57 last edited by
                      #10

                      It's working awesome on QT Simulator, but in device N8 + QtSDK 1.1 + installed all required SIS packages. I'm getting following error on this statement
                      @var rs = tx.executeSql('SELECT * FROM Greeting');@

                      Error notification
                      @[Qt Message] QSqlQuery::prepare: database not open
                      [Qt Message] Error!
                      [Qt Message] message >
                      [Qt Message] code > 1
                      [Qt Message] lineNumber > 21
                      [Qt Message] sourceId > 5921784
                      [Qt Message] fileName > file:///C:/Private/ee844ce3/qml/Sqlite/main.qml
                      [Qt Message] file:///C:/Private/ee844ce3/qml/Sqlite/main.qml:30: TypeError: Result of expression 'rs' [undefined] is not an object.@

                      1 Reply Last reply
                      0
                      • B Offline
                        B Offline
                        blam
                        wrote on 27 Jan 2011, 01:03 last edited by
                        #11

                        It looks like the database files are not accessible, and could not be created either. Can you verify whether the setting of the offline storage was successful? That is, does engine()->offlineStoragePath() point to a valid path? And is it a location that can be written to by the application?

                        Also it could be that setting the offline storage path to "qml/Sqlite" doesn't work since paths on Symbian use '' for directory separators - try using QDir::separator() instead of hard coding the separator into the string.

                        1 Reply Last reply
                        0
                        • R Offline
                          R Offline
                          raghava.chinnappa
                          wrote on 27 Jan 2011, 01:10 last edited by
                          #12

                          Do you know what I did to make it work?

                          Edited my .pro file with
                          @folder_01.source = qml/Sqlite
                          folder_01.target = qml
                          DEPLOYMENTFOLDERS = folder_01

                          folder_02.source = qml/OfflineStorage
                          folder_02.target = qml
                          DEPLOYMENTFOLDERS += folder_02@

                          And I maintained my files as @\qml
                          \sqlite
                          \main.qml
                          \OfflineStorage
                          \Databases
                          \0cbc6611f5540bd0809a388dc95a615b
                          \0cbc6611f5540bd0809a388dc95a615b.ini
                          \0cbc6611f5540bd0809a388dc95a615b.sql@

                          So rather I pointing to the custom location, just copied the sqlite files to the default location. its working fine.

                          1 Reply Last reply
                          0
                          • N Offline
                            N Offline
                            noormuhammad
                            wrote on 11 Sept 2011, 11:10 last edited by
                            #13

                            I am deploying the application on nokia device. I used this code to change path

                            QDeclarativeEngine engine;
                            engine.setOfflineStoragePath(QString("E:\"));

                            When i deploy the application on device. It creates the "Databases" folder in E drive. That Databases folder contains database-hasedname.ini. But i am unable to find the database file.

                            Few forum says that this file would be created in private folder. Where as, i am unable to access/see the private folder on symbian device. How can i put the database there? If i use "create database" query, after changing offline storage path, it gives me this error.

                            [Qt Message] QSqlQuery::prepare: database not open
                            [Qt Message] file:///C:/Private/e15a05d2/qml/NokiaApp/jscript.js:15: Error

                            I don't want to create the new database. My database has hudereds of record and i can't insert those again in a new database.

                            I used the above code to change the path of database. But this isn't working anymore. The documentation does not help in this issue. :(I am stuck with this issue.Can anyone please guide me.

                            Thanks in advance.

                            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