Skip to content

Commit b7161f5

Browse files
Jeff BrownAndroid (Google) Code Review
authored andcommitted
Merge "Report extended error information from SQLite." into jb-dev
2 parents cab5b8a + 9d25fa6 commit b7161f5

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

core/jni/android_database_SQLiteCommon.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,25 @@ void throw_sqlite3_exception(JNIEnv* env, const char* message) {
3333
*/
3434
void throw_sqlite3_exception(JNIEnv* env, sqlite3* handle, const char* message) {
3535
if (handle) {
36-
throw_sqlite3_exception(env, sqlite3_errcode(handle),
37-
sqlite3_errmsg(handle), message);
36+
// get the error code and message from the SQLite connection
37+
// the error message may contain more information than the error code
38+
// because it is based on the extended error code rather than the simplified
39+
// error code that SQLite normally returns.
40+
throw_sqlite3_exception(env, sqlite3_errcode(handle), sqlite3_errmsg(handle), message);
3841
} else {
3942
// we use SQLITE_OK so that a generic SQLiteException is thrown;
4043
// any code not specified in the switch statement below would do.
4144
throw_sqlite3_exception(env, SQLITE_OK, "unknown error", message);
4245
}
4346
}
4447

45-
/* throw a SQLiteException for a given error code */
48+
/* throw a SQLiteException for a given error code
49+
* should only be used when the database connection is not available because the
50+
* error information will not be quite as rich */
4651
void throw_sqlite3_exception_errcode(JNIEnv* env, int errcode, const char* message) {
47-
if (errcode == SQLITE_DONE) {
48-
throw_sqlite3_exception(env, errcode, NULL, message);
49-
} else {
50-
char temp[21];
51-
sprintf(temp, "error code %d", errcode);
52-
throw_sqlite3_exception(env, errcode, temp, message);
53-
}
52+
char temp[21];
53+
sprintf(temp, "error code %d", errcode);
54+
throw_sqlite3_exception(env, errcode, temp, message);
5455
}
5556

5657
/* throw a SQLiteException for a given error code, sqlite3message, and
@@ -75,6 +76,7 @@ void throw_sqlite3_exception(JNIEnv* env, int errcode,
7576
break;
7677
case SQLITE_DONE:
7778
exceptionClass = "android/database/sqlite/SQLiteDoneException";
79+
sqlite3Message = NULL; // SQLite error message is irrelevant in this case
7880
break;
7981
case SQLITE_FULL:
8082
exceptionClass = "android/database/sqlite/SQLiteFullException";

core/jni/android_database_SQLiteConnection.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ static int executeNonQuery(JNIEnv* env, SQLiteConnection* connection, sqlite3_st
444444
throw_sqlite3_exception(env,
445445
"Queries can be performed using SQLiteDatabase query or rawQuery methods only.");
446446
} else if (err != SQLITE_DONE) {
447-
throw_sqlite3_exception_errcode(env, err, sqlite3_errmsg(connection->db));
447+
throw_sqlite3_exception(env, connection->db);
448448
}
449449
return err;
450450
}
@@ -479,7 +479,7 @@ static jlong nativeExecuteForLastInsertedRowId(JNIEnv* env, jclass clazz,
479479
static int executeOneRowQuery(JNIEnv* env, SQLiteConnection* connection, sqlite3_stmt* statement) {
480480
int err = sqlite3_step(statement);
481481
if (err != SQLITE_ROW) {
482-
throw_sqlite3_exception_errcode(env, err, sqlite3_errmsg(connection->db));
482+
throw_sqlite3_exception(env, connection->db);
483483
}
484484
return err;
485485
}

0 commit comments

Comments
 (0)