android - SQLite : DEFAULT values are not set to table's columns -
i trying create table in app's sqlite database has 3 columns accept integer values. create table use following code :
db = openorcreatedatabase("helpmedatabase", context.mode_private, null); db.execsql("create table if not exists settings (\n" + "\t whatsapp \t integer not null default 1,\n" + "\t viber \t integer not null default 1,\n" + "\t vibration \t integer not null default 1\n" + ");");
the table created default values not assigned columns . have idea why happens?
sorry taking long answer. @nkorth's , @rajesh khadka's suggestions managed solve issue.
so, after table's creations put condition insert single row once.
db.execsql("create table if not exists settings (" + " whatsapp integer default 1 not null," + " viber integer default 1 not null," + " vibration integer default 1 not null" + ");"); if (isfirsttime()) { db.execsql("insert settings values (1,1,1)"); }
isfirsttime() :
private boolean isfirsttime() { sharedpreferences preferences = getpreferences(mode_private); boolean ranbefore = preferences.getboolean("ranbefore", false); if (!ranbefore) { // first time sharedpreferences.editor editor = preferences.edit(); editor.putboolean("ranbefore", true); editor.commit(); } return !ranbefore; }
this code makes sure insert executed once.
Comments
Post a Comment