From: Damyan Ivanov Date: Thu, 6 Dec 2018 20:40:50 +0000 (+0000) Subject: methods for getting/setting options stored in the database X-Git-Tag: v0.3~319 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=6c55bb9b3b8e051d61cc7ee440c2ba2dd717a06f;p=mobile-ledger.git methods for getting/setting options stored in the database --- diff --git a/app/src/main/java/net/ktnx/mobileledger/MobileLedgerDB.java b/app/src/main/java/net/ktnx/mobileledger/MobileLedgerDB.java index 350f5018..d8b84fe1 100644 --- a/app/src/main/java/net/ktnx/mobileledger/MobileLedgerDB.java +++ b/app/src/main/java/net/ktnx/mobileledger/MobileLedgerDB.java @@ -1,5 +1,6 @@ package net.ktnx.mobileledger; +import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; class MobileLedgerDB { @@ -23,4 +24,53 @@ class MobileLedgerDB { db.execSQL("create table if not exists options(name varchar, value varchar);"); db.execSQL("create unique index if not exists idx_options_name on options(name);"); } + + static int get_option_value(String name, int default_value) { + String s = get_option_value(name, String.valueOf(default_value)); + try { + return Integer.parseInt(s); + } + catch (Exception e) { + return default_value; + } + } + + static long get_option_value(String name, long default_value) { + String s = get_option_value(name, String.valueOf(default_value)); + try { + return Long.parseLong(s); + } + catch (Exception e) { + Log.d("db", "returning default long value of "+name, e); + return default_value; + } + } + + static String get_option_value(String name, String default_value) { + Log.d("db", "about fo fetch option "+name); + try (Cursor cursor = db.rawQuery("select value from options where name=?", new String[]{name})) { + if (cursor.moveToFirst()) { + String result = cursor.getString(0); + + if (result == null ) result = default_value; + + Log.d("db", "option "+name+"="+result); + return result; + } + else return default_value; + } + catch(Exception e) { + Log.d("db", "returning default value for "+name, e); + return default_value; + } + } + + static void set_option_value(String name, String value) { + Log.d("db", "setting option "+name+"="+value); + db.execSQL("insert or replace into options(name, value) values(?, ?);", new String[]{name, value}); + } + + static void set_option_value(String name, long value) { + set_option_value(name, String.valueOf(value)); + } }