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 {
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.