I followed the database creation app on http://developer.blackberry.com/native/sampleapps/ but I can't figure out how can I create the database/retrieve data when my app loads. Can someone help me with good reference books for using sqlite3 with cascades, I can't find any good source for it.
Asked
Active
Viewed 145 times
0
-
Do you want a QT only solution or a QML one? – Bojan Kogoj Jul 12 '14 at 18:23
1 Answers
2
There are a few ways, but I used this one so far (it's not perfect but good enough).
- First save customsqldatasource.cpp and customsqldatasource.h inside your
/src
directory. Open your
applicationui.cpp
and add to the top#include "customsqldatasource.h"
and add this inside ApplicationUI
to expose it to QML:
qmlRegisterType<CustomSqlDataSource>("com.myapp.data", 1, 0, "CustomSqlDataSource");
- Add
LIBS += -lbbdata
to your .pro file - add your database in
/assets
; location is up to you, just make sure it matchessource
inCustomSqlDataSource
- add
import com.myapp.data 1.0
to your.qml
file
Within attachedObjects
add this:
CustomSqlDataSource {
id: asynkDataSource
source: "sql/mydatabase.db"
query: "SELECT * FROM recent_searches GROUP BY fromCity, toCity ORDER BY id DESC"
onDataLoaded: {
if (data.length > 0) {
//use data
}
}
}
Now all you need to do is add the following line inside onCreationCompleted
to load it
asynkDataSource.load();
I hope I didn't forget anything. A few important things: /assets
folder is read only, therefore your .db is copied to /data
folder (this script does it).

Bojan Kogoj
- 5,321
- 3
- 35
- 57