3

My code looks something like this:

ContentValues values = new ContentValues();
values.put("year", year);
values.put("month", month);
database.insert(MySQLiteHelper.TABLE_DATA, null, values);

Everything works fine, but unfortunately the insert query will always return -1 on one of my user's device. My app has bugsnag so now I changed the

insert

to

database.insertOrThrow

hoping that when an error occurs on the user's phone again, bugsnag will notify me. BUT just to make sure (since I've never used insertOrThrow before; usually just insert encapsulated in try catch), is the code above sufficient? When an error happens on insert, will bugsnag capture it? Maybe my question can be summarized as 'how do I handle throw in insertOrThrow?'

imin
  • 4,504
  • 13
  • 56
  • 103
  • see [insert](http://androidxref.com/7.1.1_r6/xref/frameworks/base/core/java/android/database/sqlite/SQLiteDatabase.java#1341) and [insertOrThrow](http://androidxref.com/7.1.1_r6/xref/frameworks/base/core/java/android/database/sqlite/SQLiteDatabase.java#1367) – pskink Jul 27 '17 at 05:21
  • @pskink I've read this doc https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#insertOrThrow(java.lang.String, java.lang.String, android.content.ContentValues) before, but I'm still unsure if bugsnag will capture that. Since currently the only way for me to do this is to ask the user to install the updated app back and forth, I'm hoping to find a way that won't involve the user... if that's even possible – imin Jul 27 '17 at 05:27
  • `insertOrThrow()` will throw an exception. You should know whether you have written code that suppresses the exception before Bugsnap gets it. – CL. Jul 27 '17 at 09:11

2 Answers2

2

insertOrThrow throws a SQLException in the event of an error, which is a subclass of RuntimeException.

Bugsnag will automatically handle any uncaught exceptions in your app by registering a Thread.UncaughtExceptionHandler, so you shouldn't need to do anything else to capture the event.

fractalwrench
  • 4,028
  • 7
  • 33
  • 49
0

Make sure you are putting all the values that you have been declared during creating the table in my case that solve the problem.