Sunday, 15 May 2011

Why does my exception handler not trap Android SQLite insert error? -



Why does my exception handler not trap Android SQLite insert error? -

i'm using sqlite first time, , trying larn exception handling, i'm forcing insert error in test app. exception occurs , see written eclipse logcat output window. doesn't caught in code. i've seen other questions here beingness sure utilize right exception type, , think i've got right. thought i'm missing?

in next statement, in main activity, mytable class extends own abstractdbadapter (which has class databasehelper extends sqliteopenhelper).

try { mytable.create("dupkey"); } grab (android.database.sqlite.sqliteconstraintexception e) { log.e(tag, "sqliteconstraintexception:" + e.getmessage()); } grab (android.database.sqlite.sqliteexception e) { log.e(tag, "sqliteexception:" + e.getmessage()); } grab (exception e) { log.e(tag, "exception:" + e.getmessage()); }

sample stack trace:

error inserting id="dupkey" last_seen_ts=1360624732 first_seen_ts=1360624732 android.database.sqlite.sqliteconstraintexception: error code 19: constraint failed @ android.database.sqlite.sqlitestatement.native_execute(native method) @ android.database.sqlite.sqlitestatement.execute(sqlitestatement.java:61) @ android.database.sqlite.sqlitedatabase.insertwithonconflict(sqlitedatabase.java:1582) @ android.database.sqlite.sqlitedatabase.insert(sqlitedatabase.java:1426) @ com.mycompany.testapp.mytable_dbadapter.create(mytable_dbadapter.java:51)

the mytable , abstractdbadapter classes:

public class mytable_dbadapter extends abstractdbadapter { private static final string database_table = "mytable"; // column names -- keys contentvalues() public static final string key_id = "id"; public static final string key_first_seen = "first_seen_ts"; public static final string key_last_seen = "last_seen_ts"; public mytable_dbadapter(context ctx) { super(ctx); } public long create(string id) { long firstseen = system.currenttimemillis() / 1000; // sqlite timestamps in seconds contentvalues args = new contentvalues(); args.put(key_id, id); args.put(key_first_seen, firstseen); args.put(key_last_seen, firstseen); // defaults firstseen new entry homecoming mdb.insert(database_table, null, args); } } public abstract class abstractdbadapter { protected static final string tag = "abstractdbadapter"; protected databasehelper mdbhelper = null; protected sqlitedatabase mdb = null; protected static final string table_create_mytable = "create table mytable (" + " id text primary key not null" + ", first_seen_ts integer not null" + ", last_seen_ts integer not null" + ");"; protected static final string database_name = "mydb"; protected static final int database_version = 1; protected final context mctx; protected static class databasehelper extends sqliteopenhelper { databasehelper(context context) { super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { // note: sqlite requires 1 execsql per table db.execsql(table_create_mytable); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { log.w(tag, "upgrading database version " + oldversion + " " + newversion + ", destroys existing data."); db.execsql("drop table if exists mytable"); oncreate(db); } } public abstractdbadapter(context ctx) { this.mctx = ctx; } public abstractdbadapter open() throws sqlexception { mdbhelper = new databasehelper(mctx); mdb = mdbhelper.getwritabledatabase(); homecoming this; } public void close() { if (mdb != null) { mdb.close(); mdb = null; } if (mdbhelper != null) { mdbhelper.close(); mdbhelper = null; } } }

i found reply here: sqliteconstraintexception not caught

the sqlitedatabase.insert() method doesn't throw exception. d'oh!

for other sqlite newbies me, if want grab exceptions when inserting database, utilize sqlite.insertorthrow() method. throw exception can grab , handle.

android sqlite exception-handling

No comments:

Post a Comment