Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

Local Storage

The local storage API provides a JavaScript interface to an SQL relational database. The QtQuick.LocalStorage module contains the API and it may be given a namespace.

Import QtQuick.LocalStorage module from QML:

 //sql.qml

 import QtQuick.LocalStorage 2.0 as Sql

Import QtQuick.LocalStorage module from JavaScript:

 //sql.js
 .import QtQuick.LocalStorage 2.0 as Sql

Note, importing a module from JavaScript is different from importing from QML. The JavaScript Code article contains detailed information on importing in JavaScript code.

Database API

The openDatabaseSync() and related functions provide the ability to access local storage in an SQL database.

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:


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 Local Storage API.

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

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.

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 5.0-snapshot
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web