January 21, 2011

raghava.chinnappa raghava.chin..
Lab Rat
21 posts

Loading Sqlite database within the project directory

 

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

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

  1.     QDeclarativeEngine engine;
  2.     QDir dir;
  3.     QString customPath = "qml/Sqlite-Example";
  4.  
  5.     if(dir.mkpath(QString(customPath))){
  6.         engine.setOfflineStoragePath(QString(customPath));
  7.     }
  8.  
  9.     QString str = engine.offlineStoragePath();
  10.     qDebug() << str;

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

12 replies

January 25, 2011

blam blam
Lab Rat
58 posts

The files need to be within a “Databases” subdirectory.

January 25, 2011

raghava.chinnappa raghava.chin..
Lab Rat
21 posts

I tried that option also. Didn’t worked!

January 25, 2011

mbrasser mbrasser
Ant Farmer
452 posts

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

January 25, 2011

raghava.chinnappa raghava.chin..
Lab Rat
21 posts

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

January 25, 2011

mbrasser mbrasser
Ant Farmer
452 posts

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

January 25, 2011

raghava.chinnappa raghava.chin..
Lab Rat
21 posts

The Project folder structure is like this

  1. + sqlite
  2.      \Databases
  3.          \0cbc6611f5540bd0809a388dc95a615b
  4.          \0cbc6611f5540bd0809a388dc95a615b.ini
  5.          \0cbc6611f5540bd0809a388dc95a615b.sql
  6.      \main.qml

Source code for main.cpp

  1. #include <QtGui/QApplication>
  2. #include "qmlapplicationviewer.h"
  3. #include <QDebug>
  4. #include <QDeclarativeEngine>
  5. #include <QDir>
  6. #include <QString>
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10.     QApplication app(argc, argv);
  11.  
  12.  
  13.     QDeclarativeEngine engine;
  14.     QString customPath = "qml/Sqlite";
  15.  
  16.     QDir dir;
  17.     if(dir.mkpath(QString(customPath))){
  18.         qDebug() << "Default path >> "+engine.offlineStoragePath();
  19.         engine.setOfflineStoragePath(QString(customPath));
  20.         qDebug() << "New path >> "+engine.offlineStoragePath();
  21.     }
  22.  
  23.     QmlApplicationViewer viewer;
  24.     viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
  25.     viewer.setMainQmlFile(QLatin1String("qml/Sqlite/main.qml"));
  26.     viewer.showExpanded();
  27.  
  28.     return app.exec();
  29. }

source code for main.qml is

  1. import Qt 4.7
  2.  
  3. Rectangle {
  4.      color: "#ffffff"
  5.      width: 200
  6.      height: 100
  7.  
  8.      Text {
  9.          id: textArea
  10.          text: "?"
  11.          anchors.horizontalCenter: parent.horizontalCenter
  12.          Component.onCompleted: findGreetings()
  13.      }
  14.  
  15.      function findGreetings() {
  16.          var db = openDatabaseSync("Test", "1.0", "The Example QML SQL!", 1000000);
  17.          db.transaction(
  18.              function(tx) {
  19.  
  20.                  // Show all added greetings
  21.                  var rs = tx.executeSql('SELECT * FROM Greeting');
  22.  
  23.                  var r = ""
  24.                  for(var i = 0; i < rs.rows.length; i++) {
  25.                      r += rs.rows.item(i).salutation + ", " + rs.rows.item(i).salutee + "\n";
  26.                  }
  27.                  print(r);
  28.                  textArea.text = r;
  29.              }
  30.          )
  31.      }
  32.  
  33.  }

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

  1. "Mr.",  "Apple"
  2. "Mr.", "Ball"
  3. "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.

January 25, 2011

jpetrell jpetrell
Lab Rat
3 posts

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

  1. QmlApplicationViewer viewer;
  2. viewer.engine()->setOfflineStoragePath(QString(customPath));

January 25, 2011

raghava.chinnappa raghava.chin..
Lab Rat
21 posts

Wow, It worked finally. Thanks lot @jpetrell

January 26, 2011

raghava.chinnappa raghava.chin..
Lab Rat
21 posts

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

  1. var rs = tx.executeSql('SELECT * FROM Greeting');

Error notification

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

January 27, 2011

blam blam
Lab Rat
58 posts

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.

January 27, 2011

raghava.chinnappa raghava.chin..
Lab Rat
21 posts

Do you know what I did to make it work?

Edited my .pro file with

  1. folder_01.source = qml/Sqlite
  2. folder_01.target = qml
  3. DEPLOYMENTFOLDERS = folder_01
  4.  
  5. folder_02.source = qml/OfflineStorage
  6. folder_02.target = qml
  7. DEPLOYMENTFOLDERS += folder_02

And I maintained my files as

  1. \qml
  2.  \sqlite
  3.    \main.qml
  4.  \OfflineStorage
  5.    \Databases
  6.     \0cbc6611f5540bd0809a388dc95a615b
  7.     \0cbc6611f5540bd0809a388dc95a615b.ini
  8.     \0cbc6611f5540bd0809a388dc95a615b.sql

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

September 11, 2011

noormuhammad noormuhammad
Lab Rat
3 posts

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.

 
  ‹‹ Get screen size from QML      OpenGL Requirements? ››

You must log in to post a reply. Not a member yet? Register here!