4 // First, let's create a short helper function to get the database connection
5 function getDatabase() {
6 return openDatabaseSync("QuickNewsReader", "1.0", "SettingsStorageDatabase", 100000);
9 // At the start of the application, we can initialize the tables we need if they haven't been created yet
10 function initialize() {
11 var db = getDatabase();
14 // Create the settings table if it doesn't already exist
15 // If the table exists, this is skipped
16 tx.executeSql('CREATE TABLE IF NOT EXISTS settings(setting TEXT UNIQUE, value TEXT)');
20 // This function is used to write a setting into the database
21 function setSetting(setting, value) {
22 // setting: string representing the setting name (eg: “username”)
23 // value: string representing the value of the setting (eg: “myUsername”)
24 var db = getDatabase();
26 db.transaction(function(tx) {
27 var rs = tx.executeSql('INSERT OR REPLACE INTO settings VALUES (?,?);', [setting,value]);
28 //console.log(rs.rowsAffected)
29 if (rs.rowsAffected > 0) {
36 // The function returns “OK” if it was successful, or “Error” if it wasn't
40 // This function is used to retrieve a setting from the database
41 function getSetting(setting) {
42 var db = getDatabase();
44 db.transaction(function(tx) {
45 var rs = tx.executeSql('SELECT value FROM settings WHERE setting=?;', [setting]);
46 if (rs.rows.length > 0) {
47 res = rs.rows.item(0).value;
52 // The function returns “Unknown” if the setting was not found in the database
53 // For more advanced projects, this should probably be handled through error codes