aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorNico Vertriest <nico.vertriest@qt.io>2016-11-09 14:52:41 +0100
committerNico Vertriest <nico.vertriest@theqtcompany.com>2016-12-20 09:44:01 +0000
commit0577aa9fd2808d24631ae03a9e3cbe6326be20e6 (patch)
tree4c04f35d4ef3452ecb9f8d5f0a5af1a01da28c38 /src
parent16c81bb0d493af00bc376784bcb7e03a4a037b04 (diff)
Doc: added support for JSON in localstorage documentation
Added examples on how to store in JSON format Change-Id: Ief58e28d42cd87cc0829e9265670e7c7bbdbeffe Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/imports/localstorage/plugin.cpp22
-rw-r--r--src/quick/doc/snippets/qml/localstorage/dbtransaction.js30
2 files changed, 41 insertions, 11 deletions
diff --git a/src/imports/localstorage/plugin.cpp b/src/imports/localstorage/plugin.cpp
index cd6d37ea5a..6704283cb9 100644
--- a/src/imports/localstorage/plugin.cpp
+++ b/src/imports/localstorage/plugin.cpp
@@ -640,14 +640,32 @@ Below you will find an example of a database transaction which catches exception
\snippet qml/localstorage/dbtransaction.js 0
+In the example you can see an \c insert statement where values are assigned to the fields,
+and the record is written into the table. That is an \c insert statement with a syntax that is usual
+for a relational database. It is however also possible to work with JSON objects and
+store them in a table.
+
+Let's suppose a simple example where we store trips in JSON format using \c date as the unique key.
+An example of a table that could be used for that purpose:
+
+\snippet qml/localstorage/dbtransaction.js 3
+
+The assignment of values to a JSON object:
+
+\snippet qml/localstorage/dbtransaction.js 4
+
+In that case, the data could be saved in the following way:
+
+\snippet qml/localstorage/dbtransaction.js 5
+
\section3 db.readTransaction(callback(tx))
This method creates a read-only transaction and passed to \e callback. In this function,
-you can call \e executeSql on \e tx to read the database (with SELECT statements).
+you can call \e executeSql on \e tx to read the database (with \c select statements).
\section3 results = tx.executeSql(statement, values)
-This method executes a SQL \e statement, binding the list of \e values to SQL positional parameters ("?").
+This method executes an SQL \e statement, binding the list of \e values to SQL positional parameters ("?").
It returns a results object, with the following properties:
diff --git a/src/quick/doc/snippets/qml/localstorage/dbtransaction.js b/src/quick/doc/snippets/qml/localstorage/dbtransaction.js
index 40eb6d2804..38733a8e2c 100644
--- a/src/quick/doc/snippets/qml/localstorage/dbtransaction.js
+++ b/src/quick/doc/snippets/qml/localstorage/dbtransaction.js
@@ -43,11 +43,11 @@ var db = LocalStorage.openDatabaseSync("ActivityTrackDB", "", "Database tracking
db.transaction(
try {
function(tx) {
- tx.executeSql('INSERT INTO trip_log VALUES(?, ?, ?)',
- [ '01/10/2016','Sylling - Vikersund', '53' ]);
+ tx.executeSql("INSERT INTO trip_log VALUES(?, ?, ?)",
+ [ "01/10/2016","Sylling - Vikersund", "53" ]);
}
} catch (err) {
- console.log("Error inserting into table Greeting: " + err);
+ console.log("Error inserting into table trip_log: " + err);
}
)
//![0]
@@ -60,11 +60,11 @@ function db_distance_select(Pdistance)
var db = LocalStorage.openDatabaseSync("ActivityTrackDB", "", "Database tracking sports activities", 1000000);
db.transaction(
function(tx) {
- var results = tx.executeSql('SELECT rowid,
+ var results = tx.executeSql("SELECT rowid,
date,
trip_desc,
distance FROM trip_log
- where distance >= ?',[Pdistance]');
+ where distance >= ?",[Pdistance]);
for (var i = 0; i < results.rows.length; i++) {
listModel.append({"id": results.rows.item(i).rowid,
"date": results.rows.item(i).date,
@@ -76,10 +76,22 @@ db.transaction(
//![1]
//![2]
var db = LocalStorage.openDatabaseSync("ActivityTrackDB", "", "Database tracking sports activities", 1000000);
-if (db.version == '0.1') {
- db.changeVersion('0.1', '0.2', function(tx) {
- tx.executeSql('INSERT INTO trip_log VALUES(?, ?, ?)',
- [ '01/10/2016','Sylling - Vikersund', '53' ]);
+if (db.version == "0.1") {
+ db.changeVersion("0.1", "0.2", function(tx) {
+ tx.executeSql("INSERT INTO trip_log VALUES(?, ?, ?)",
+ [ "01/10/2016","Sylling - Vikersund", "53" ]);
}
});
//![2]
+//![3]
+create table trip_log(date text, data text)
+//![3]
+//![4]
+var obj = {description = "Vikersund - Noresund", distance = "60"}
+//![4]
+//![5]
+db.transaction(function(tx) {
+ result = tx.executeSQL("insert into trip_log values (?,?)",
+ ["01/11/2016", JSON.stringify(obj)])
+}
+//![5]