Mangiare Senza Glutine disponibile su App Store

Per altre informazioni scrivi a fabriziocaldarelli@negusweb.it

(English) Database SQLite in Qt Symbian and QML

Da Programmazione Software.

Use SQLite database in Qt can be done in two method.

1) Pure Qt. In this case, in .pro file have to specified .sqlite file path:

sqlite_folder.source = sqlite
DEPLOYMENTFOLDERS += sqlite_folder

"sqlite" folder is where we'll save file db_name.sqlite.

Again, in .pro file remember to add

QT += sql 

to link SQL library to Qt project.

Next, to use db:

    QString privatePathQt(QApplication::applicationDirPath());
    QString path(privatePathQt);
    path.append(QDir::separator()).append("sqlite");
    path = QDir::toNativeSeparators(path);
 
#ifdef Q_WS_SIMULATOR
    path = "sqlite"
#endif
 
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    qDebug() << path;
    db.setDatabaseName(path); 
    bool ok = db.open();
    qDebug() << db.tables();

note compilation condition which you don't specify absolute path when you use simulator.


2) Pure QML. In .pro file you have to specify .sqlite path:

sqlite_folder.source = sqlite
DEPLOYMENTFOLDERS += sqlite_folder

"sqlite" folder must contain "Databases" subfolder, where we'll save db_name.sqlite

Again, different from method 1), here the files are two and setted so:

md5(db_name).sqlite
md5(db_name).ini

with db name (that we'll call in QML file) have to saved with md5 method.

For example, if our db_name is "pippo.sqlite", filename to save in "sqlite\Databases" will

0c88028bf3aa6a6a143ed846f2be1ea4.sqlite

In file .ini, with same name as previous, have to contain six rows:

[General]
Name=<db_name>
Version=1.0
Description=<description>
EstimatedSize=1000000
Driver=QSQLITE

Again, always in .pro file remember to add

QT += sql 

to link SQL library to Qt project.

Next to use db:

    QDeclarativeView *declarativeView = new QDeclarativeView(this);
 
#ifdef Q_WS_SIMULATOR
    declarativeView->engine()->setOfflineStoragePath("sqlite");
#else
    QString privatePathQt(QApplication::applicationDirPath());
    QString pathOfflineStorage(privatePathQt);
    pathOfflineStorage.append(QDir::separator()).append("sqlite");
    pathOfflineStorage = QDir::toNativeSeparators(pathOfflineStorage);
    declarativeView->engine()->setOfflineStoragePath(pathOfflineStorage);
#endif
    declarativeView->setSource(QUrl::fromLocalFile("qml/Starter.qml"));
    declarativeView->showFullScreen();

note compilation condition which you don't specify absolute path when you use simulator.

In QML file:

function openDatabase()
{
    var db = openDatabaseSync("db_release", "1.0", "The Example QML SQL!", 1000000);
    return db;
}
 
function loadRecordList()
{
    var db = openDatabase();
    var arrOut = new Array();
 
    db.transaction(
        function(tx) {
            var strSql = "SELECT * FROM table";
            var rs = tx.executeSql(strSql);
            console.log(strSql)
 
            for(var i = 0; i < rs.rows.length; i++) {
                arrOut.push(rs.rows.item(i))
            }
 
        }
    )
    return arrOut;
}