Qt Quick Local Storage QML Types

This is a singleton type for reading and writing to SQLite databases.

Methods

  • object openDatabaseSync(string name, string version, string description, int estimated_size, jsobject callback(db))

Detailed Description

To use the types in this module, import the module and call the relevant functions using the LocalStorage type:

import QtQuick.LocalStorage 2.0
import QtQuick 2.0

Item {
    Component.onCompleted: {
        var db = LocalStorage.openDatabaseSync(...)
    }
}

These databases are user-specific and QML-specific, but accessible to all QML applications. They are stored in the Databases subdirectory of QQmlEngine::offlineStoragePath(), currently as SQLite databases.

Database connections are automatically closed during Javascript garbage collection.

The API can be used from JavaScript functions in your QML:

import QtQuick 2.0
import QtQuick.LocalStorage 2.0

Rectangle {
    width: 200
    height: 100

    Text {
        text: "?"
        anchors.horizontalCenter: parent.horizontalCenter

        function findGreetings() {
            var db = LocalStorage.openDatabaseSync("QQmlExampleDB", "1.0", "The Example QML SQL!", 1000000);

            db.transaction(
                function(tx) {
                    // Create the database if it doesn't already exist
                    tx.executeSql('CREATE TABLE IF NOT EXISTS Greeting(salutation TEXT, salutee TEXT)');

                    // Add (another) greeting row
                    tx.executeSql('INSERT INTO Greeting VALUES(?, ?)', [ 'hello', 'world' ]);

                    // Show all added greetings
                    var rs = tx.executeSql('SELECT * FROM Greeting');

                    var r = ""
                    for(var i = 0; i < rs.rows.length; i++) {
                        r += rs.rows.item(i).salutation + ", " + rs.rows.item(i).salutee + "\n"
                    }
                    text = r
                }
            )
        }

        Component.onCompleted: findGreetings()
    }
}

The API conforms to the Synchronous API of the HTML5 Web Database API, W3C Working Draft 29 October 2009.

The SQL Local Storage example demonstrates the basics of using the Offline Storage API.

Open or create a databaseData
import QtQuick.LocalStorage 2.0 as Sql

db = Sql.openDatabaseSync(identifier, version, description, estimated_size, callback(db))

The above code returns the database identified by identifier. If the database does not already exist, it is created, and the function callback is called with the database as a parameter. description and estimated_size are written to the INI file (described below), but are otherwise currently unused.

May throw exception with code property SQLException.DATABASE_ERR, or SQLException.VERSION_ERR.

When a database is first created, an INI file is also created specifying its characteristics:

KeyValue
NameThe name of the database passed to openDatabase()
VersionThe version of the database passed to openDatabase()
DescriptionThe description of the database passed to openDatabase()
EstimatedSizeThe estimated size (in bytes) of the database passed to openDatabase()
DriverCurrently "QSQLITE"

This data can be used by application tools.

db.changeVersion(from, to, callback(tx))

This method allows you to perform a Scheme Upgrade.

If the current version of db is not from, then an exception is thrown.

Otherwise, a database transaction is created and passed to callback. In this function, you can call executeSql on tx to upgrade the database.

May throw exception with code property SQLException.DATABASE_ERR or SQLException.UNKNOWN_ERR.

db.transaction(callback(tx))

This method creates a read/write transaction and passed to callback. In this function, you can call executeSql on tx to read and modify the database.

If the callback throws exceptions, the transaction is rolled back.

db.readTransaction(callback(tx))

This method creates a read-only transaction and passed to callback. In this function, you can call executeSql on tx to read the database (with SELECT statements).

results = tx.executeSql(statement, values)

This method executes a SQL statement, binding the list of values to SQL positional parameters ("?").

It returns a results object, with the following properties:

TypePropertyValueApplicability
introws.lengthThe number of rows in the resultSELECT
varrows.item(i)Function that returns row i of the resultSELECT
introwsAffectedThe number of rows affected by a modificationUPDATE, DELETE
stringinsertIdThe id of the row insertedINSERT

May throw exception with code property SQLException.DATABASE_ERR, SQLException.SYNTAX_ERR, or SQLException.UNKNOWN_ERR.

Method Documentation

object openDatabaseSync(string name, string version, string description, int estimated_size, jsobject callback(db))

Opens or creates a local storage sql database by the given parameters.

  • name is the database name
  • version is the database version
  • description is the database display name
  • estimated_size is the database's estimated size, in bytes
  • callback is an optional parameter, which is invoked if the database has not yet been created.

Returns the created database object.

© 2015 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.