Loading Sqlite database within the project directory



  • 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

    1. Qt.md5(“mydb_name”).sqlite
    2. 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?



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



  • I tried that option also. Didn't worked!



  • 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



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



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



  • 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.



  • 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));@



  • Wow, It worked finally. Thanks lot @jpetrell



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



  • 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.



  • 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.



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.