Android When does insertOrThrow () throw an exception?

Before reading the documentation, I would expect that the SQLiteDatabase insertOrThrow() method should have excluded if the insert was not successful.

However, the documentation says that the insertOrThrow() method

returns the row id of the row just inserted or -1 if an error occurred

So, if it returns -1 when an error occurs, under what circumstances does it throw an exception?

+5
source share
3 answers

I understand that it will return -1 if all the code can be executed correctly and a "EXPECTED" error has occurred. However, not all errors / exceptions are handled, and some exceptions may still be thrown:

  • SQLiteDatabaseCorruptException
  • IllegalArgumentException
  • Runtimeexception
  • Database is open for reading only.
  • Attempting to insert a duplicate key

HERE you can find the implementation of insert() and insertOrThrow() .

You can see that both methods are similar. However, insert() handles a SQLException for you. insertOrThrow() does not handle any exceptions, and you must do it yourself.

 public long insert(String table, String nullColumnHack, ContentValues values) { try { return insertWithOnConflict(table, nullColumnHack, values, CONFLICT_NONE); } catch (SQLException e) { Log.e(TAG, "Error inserting " + values, e); return -1; } } public long insertOrThrow(String table, String nullColumnHack, ContentValues values) throws SQLException { return insertWithOnConflict(table, nullColumnHack, values, CONFLICT_NONE); } 

Both methods call insertWithOnConflict() , the implementation of which is HERE :

 public long insertWithOnConflict(String table, String nullColumnHack, ContentValues initialValues, int conflictAlgorithm) { acquireReference(); try { // CODE try { // ONLY HERE I return some value.... Otherwise, I'll raise an exception return statement.executeInsert(); } finally { statement.close(); } } finally { releaseReference(); } } 

In the implementation, you will see that it returns "-1" only if statement.executeInsert(); returns something (and it seems like β€œ-1” came from here):

... / framework / base / core /JNI/android_database_SQLiteConnection.cpp

 static jlong nativeExecuteForLastInsertedRowId(JNIEnv* env, jclass clazz, jlong connectionPtr, jlong statementPtr) { SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr); sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr); int err = executeNonQuery(env, connection, statement); return err == SQLITE_DONE && sqlite3_changes(connection->db) > 0 ? sqlite3_last_insert_rowid(connection->db) : -1; } 

If any other error / exception occurs in the middle, it throws an exception.

If you follow the called methods, you can see that other errors are not being processed. In addition, you will notice that SQLite will actually be executed by the job C (and not java). Thus, several unforeseen errors are possible.

+3
source

You can find the source code for SQLiteDatabase to find out how it works. This seems to result in a SQLiteDatabaseCorruptException if the SQL string is invalid, otherwise returns -1.

0
source

The insertOrThrow () method throws an exception and returns -1. This way you can check if your request was satisfactory or not. If it returns -1, your action has not been completed, and you need to find out what went wrong!

-1
source

All Articles