Saving integers on an offline storage triggered by mousearea
-
Im trying to save a number previewed from a text element and i want to save it in an offline storage triggered when the mousearea is clicked,
i want to save that number beacause i also want to load it on another QML,
Can you please help me,@import QtQuick 1.0
import "storage.js" as StorageRectangle {
color: "white"
width: 360
height: 490Text { id: wa text: "75"
}
MouseArea {
id: schoose2name
x: 5
y: 406
width: 103
height: 77
onClicked: {//When this mousearea is clicked the number 75 on wa.text will be saved on can be updated in my offline storage
}}@
@//storage.js
// First, let's create a short helper function to get the database connection
function getDatabase() {
return openDatabaseSync("MyAppName", "1.0", "StorageDatabase", 10000000);
}// At the start of the application, we can initialize the tables we need if they haven't been created yet
function initialize() {
var db = getDatabase();
db.transaction(
function(tx) {
// Create the settings table if it doesn't already exist
// If the table exists, this is skipped
tx.executeSql('CREATE TABLE IF NOT EXISTS settings(setting TEXT UNIQUE, value TEXT)');
});
}// This function is used to write a setting into the database
function setSetting(setting, value) {
// setting: string representing the setting name (eg: “username”)
// value: string representing the value of the setting (eg: “myUsername”)
var db = getDatabase();
var res = "";
db.transaction(function(tx) {
var rs = tx.executeSql('INSERT OR REPLACE INTO settings VALUES (?,?);', [setting,value]);
//console.log(rs.rowsAffected)
if (rs.rowsAffected > 0) {
res = "OK";
} else {
res = "Error";
}
}
);@ -
It's all about calling the JS functions from the Storage variable, that you bound to the JavaScript file with the code.
First initialize the DB when your "main" component has been loaded. Hook to the onCompleted signal (http://doc.qt.nokia.com/4.7-snapshot/qml-component.html#onCompleted-signal). In your example, I guess the main Rectangle (without an id):
@Component.onCompleted: { Storage.initialize() }@
Then hook to the mouse press signal:
@
onClicked: { Storage.setSetting("settingName", "value"); }@I didn't test if this actually works - but I hope you get the idea :)
-
[quote author="jr_jags" date="1312090892"]onClicked: { Storage.setSetting(wa.text, "value"); }
so in the settingName i put the wa.text to save the text in wa.text, is that how you can save it?[/quote]
That would store a setting that has the name of the value of wa.text (that's "75") and put the string "value" in it. So I think what you really want to do it swap the two parameters and do something like
@Storage.setSetting("settingName", wa.text)@
Now you have a setting with the key "settingName" the value "75". But you might want to change the "settingName" to something that suits your use case - depending on what wa.text actually represents.
[quote author="jr_jags" date="1312090892"]
How about loading it?
should i use tx.executeSQL(SELECT settingName) ?
[/quote]There's a pretty good documentation about off-line storage in the link below which I recommend you to read. The page also contains an example on how to read data from the DB. One pre-requisite is that you know SQL to be able to query the SQL database.
http://doc.qt.nokia.com/master-snapshot/qdeclarativeglobalobject.html#offline-storage-apiThe SameGame example also uses the off-line storage, which demonstrates how to read from one:
http://doc.qt.nokia.com/master-snapshot/declarative-tutorials-samegame-samegame4.html#storing-high-scores-offline