2 ** Copyright (C) 2012 Christophe CHAPUIS <chris.chapuis _at_ gmail _dot_ com>
4 ** This package is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License as published by
6 ** the Free Software Foundation; either version 2 of the License, or
7 ** (at your option) any later version.
9 ** This package is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ** GNU General Public License for more details.
14 ** You should have received a copy of the GNU General Public License
15 ** along with this package; if not, write to the Free Software
16 ** Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 // First, let's create a short helper function to get the database connection
23 function getDatabase() {
24 return openDatabaseSync("QuickNewsReader", "1.0", "SettingsStorageDatabase", 100000);
27 // At the start of the application, we can initialize the tables we need if they haven't been created yet
28 function initialize() {
29 var db = getDatabase();
32 // Create the settings table if it doesn't already exist
33 // If the table exists, this is skipped
34 tx.executeSql('CREATE TABLE IF NOT EXISTS settings(setting TEXT UNIQUE, value TEXT)');
38 // This function is used to write a setting into the database
39 function setSetting(setting, value) {
40 // setting: string representing the setting name (eg: “username”)
41 // value: string representing the value of the setting (eg: “myUsername”)
42 var db = getDatabase();
44 db.transaction(function(tx) {
45 var rs = tx.executeSql('INSERT OR REPLACE INTO settings VALUES (?,?);', [setting,value]);
46 //console.log(rs.rowsAffected)
47 if (rs.rowsAffected > 0) {
54 // The function returns “OK” if it was successful, or “Error” if it wasn't
58 // This function is used to retrieve a setting from the database
59 function getSetting(setting) {
60 var db = getDatabase();
62 db.transaction(function(tx) {
63 var rs = tx.executeSql('SELECT value FROM settings WHERE setting=?;', [setting]);
64 if (rs.rows.length > 0) {
65 res = rs.rows.item(0).value;
70 // The function returns “Unknown” if the setting was not found in the database
71 // For more advanced projects, this should probably be handled through error codes