PDA

View Full Version : QT QML OfflineDatabaseAPI



bmn
7th August 2011, 16:00
I'm currently creating a program similar to Are you smarter than a 5th grader, where questions should be randomly chosen by the program.

If ever I use the OfflineDatabaseAPI to store my questions, how do I save the database into a file where I can access it or how do I save the database so that when the application is installed to a mobile phone the database will still be accessible?

raja26
9th August 2011, 11:40
If you are using latest Qt Creator i.e. Qt SDK 1.2, you will see these lines in your project pro file.

# Add more folders to ship with the application, here
folder_01.source = qml/QtQuickDemo
folder_01.target = qml
DEPLOYMENTFOLDERS = folder_01

use this facility to package and export your database files..

Else:
Create a database file during the first startup of the application.. And if the database is already present then do nothing..

This method is quite unwanted and indirect..

bmn
9th August 2011, 12:15
Hello,

Yes, my .pro file looks like that. However, I really don't have any knowledge on how to package/export database files. I don't even know where the database file is :(

bmn
9th August 2011, 17:11
Okay, so managed to stumble upon this.

http://developer.qt.nokia.com/forums/viewthread/2093

Basically, it helped out a lot. Although I have some questions for you pro's.

1. The dude who made the thread in that link stated that he created three files.

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

I saw the .ini and the .sqlite file. But I didn't see that folder. Is that even required? And where do you declare these Qt.md5(“mydb_name”).ini? Inside your .cpp?

2. I did exactly what he did

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

where my custom path is set to "qml/OfflineStorage"
I created that qml/OfflineStorage path manually by navigating to the project folder and just creating a new folder, renaming it OfflineStorage and there's where I copied and pasted the .ini and .sqlite files I found on the default location. Is that right?

Here's the structure of my project:


\qml
\Projectproj
\main.qml
\data.txt
\OfflineStorage
\Databases
\9a41722cf143cae53004c21c7ec7b42f.ini
\9a41722cf143cae53004c21c7ec7b42f.sql


I also made sure that my qrc file was updated.


<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file>qml/Projectproj/data.txt</file>
<file>qml/OfflineStorage/Databases/9a41722cf143cae53004c21c7ec7b42f.ini</file>
<file>qml/OfflineStorage/Databases/9a41722cf143cae53004c21c7ec7b42f.sqlite</file>
</qresource>
</RCC>



Is that right? Do I include the Databases folder in the filepath?

And when I ran the application, it didn't read the database stored in the default location (C:/Users/...) since i set the custom path to qml/OfflineStorage

Here's my main.cpp


QApplication app(argc, argv);
QDeclarativeEngine engine;
QString customPath = "qml/OfflineStorage";
QmlApplicationViewer viewer;
viewer.engine()->setOfflineStoragePath(QString(customPath));
viewer.setOrientation(QmlApplicationViewer::Screen OrientationLockPortrait);
viewer.setMainQmlFile(QLatin1String("qml/Projectproj/main.qml"));
viewer.showExpanded();
return app.exec();


I wanna know what I'm doing wrong.

EDIT:

Well, I finally got it to work. The database opens. ALTHOUGH, how come I can't SAVE any data to my database?? When I use the default path -> C:/Users... etc, and I try to INSERT, it works. But when I set the customOfflineStoragePath, it does INSERT yes, but when I close the application and run it again, the database didn't change at all. How come?

bmn
10th August 2011, 08:46
Well, I finally got it to work. The database opens. ALTHOUGH, how come I can't SAVE any data to my database?? When I use the default path -> C:/Users... etc, and I try to INSERT, it works. But when I set the customOfflineStoragePath, it does INSERT yes, but when I close the application and run it again, the database didn't change at all. How come?

wysota
10th August 2011, 09:52
Because resource files are read-only. Stop shooting blind and start reading the documentation instead of gathering incoherent code snippets from all over the web trying to stitch them all toghether to complete your school assignment.