0

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.

Bojan Kogoj
  • 5,321
  • 3
  • 35
  • 57
KKa
  • 408
  • 4
  • 19

1 Answers1

2

There are a few ways, but I used this one so far (it's not perfect but good enough).

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 matches source in CustomSqlDataSource
  • 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