Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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, Maythux, Radu Rădeanu, Avinash Raj Apr 16 '14 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.

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"


    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 {
                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) {
        db.transaction( function(tx){
            tx.executeSql('INSERT OR REPLACE INTO settings VALUES(?, ?)', [key, value]);

    function getSetting(key) {
        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

        Column {
            anchors.fill: parent

            OptionSelector {
                id: distroToggle
                text:"Favorite Distro")
                model: ["Ubuntu"),"Debian")]

            OptionSelector {
                id: fooToggle
                model: ["Bar"),"Baz")]

            Button {
                text:"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
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.