Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

This question already has an answer here:

I have not been able to find a working example in QT creator or gallery to use SQLite offline storage API for an Ubuntu touch application.

share|improve this question

marked as duplicate by Jorge Castro, Eric Carvalho, Ask, Radu Rădeanu, Avinash Raj Apr 16 at 16:31

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers 2

You can us the SQLite offline storage API directly from Qml using Javascript:

import QtQuick.LocalStorage 2.0

If you want in-depth examples, I'd suggest looking at the source of some of the Ubuntu Touch Core-Apps. I know that at least the weather, clock, and RSS reader apps all use this. For now though, here's a simple example:

import QtQuick.LocalStorage 2.0
import QtQuick 2.0
import Ubuntu.Components 0.1

MainView {
    id: root
    // Note: applicationName sets the storage path
    applicationName: "com.ubuntu.developer.andrewsomething.example-app"

    width: units.gu(50)
    height: units.gu(75)

    property var db: null

    function openDB() {
        if(db !== null) return;

        // db = LocalStorage.openDatabaseSync(identifier, version, description, estimated_size, callback(db))
        db = LocalStorage.openDatabaseSync("example-app", "0.1", "Simple example app", 100000);

        try {
            db.transaction(function(tx){
                tx.executeSql('CREATE TABLE IF NOT EXISTS settings(key TEXT UNIQUE, value TEXT)');
                var table  = tx.executeSql("SELECT * FROM settings");
                // Seed the table with default values
                if (table.rows.length == 0) {
                    tx.executeSql('INSERT INTO settings VALUES(?, ?)', ["distro", "Ubuntu"]);
                    tx.executeSql('INSERT INTO settings VALUES(?, ?)', ["foo", "Bar"]);
                    console.log('Settings table added');
                };
            });
        } catch (err) {
            console.log("Error creating table in database: " + err);
        };
    }


    function saveSetting(key, value) {
        openDB();
        db.transaction( function(tx){
            tx.executeSql('INSERT OR REPLACE INTO settings VALUES(?, ?)', [key, value]);
        });
    }

    function getSetting(key) {
        openDB();
        var res = "";
        db.transaction(function(tx) {
            var rs = tx.executeSql('SELECT value FROM settings WHERE key=?;', [key]);
            res = rs.rows.item(0).value;
        });
        return res;
    }

    Page {
        id: app
        title: i18n.tr("Settings")

        Column {
            anchors.fill: parent
            anchors.margins: units.gu(5)
            spacing: units.gu(2)

            OptionSelector {
                id: distroToggle
                text: i18n.tr("Favorite Distro")
                model: [i18n.tr("Ubuntu"), i18n.tr("Debian")]
            }

            OptionSelector {
                id: fooToggle
                text: i18n.tr("Foo")
                model: [i18n.tr("Bar"), i18n.tr("Baz")]
            }

            Button {
                text: i18n.tr("Save settings")
                onClicked: {
                    var distro = (distroToggle.selectedIndex === 0) ? "Ubuntu" : "Debian";
                    console.log("Saved " + distro);
                    saveSetting("distro", distro);

                    var foo = (fooToggle.selectedIndex === 0) ? "Bar" : "Baz";
                    console.log("Saved " + foo);
                    saveSetting("foo", foo);
                }
            }
        }

        Component.onCompleted: {
            var distro = getSetting('distro');
            distroToggle.selectedIndex = (distro === "Debian") ? 1 : 0;
            var foo = getSetting('foo');
            fooToggle.selectedIndex = (foo === "Baz") ? 1 : 0;
        }
    }
}

It sets up a database and some functions to get and save key/value pairs. Then it provides an interface using the Ubuntu OptionSelector components.

settings dialog

share|improve this answer

I tried an example of SQLite with the QML and the C++. You can load data with the C++ code and then send it to the QML code

share|improve this answer
1  
Can you share that example? I want to build an application for ubuntu touch (tablet or phone) using mainly QML code as my C++ skills are not that advanced –  Hpcolus Oct 2 '13 at 22:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.