Reputation: 128
I am working with SQLite android where I am using an sqLiteDatabase.insert function to insert customers and sqLiteDatabase.update to update customers and getting the result which is always a long variable, When result is -1, I know the database operation was not successful but I want to display the actual SQLException that occurred. As the return type of these methods is long so they only print the exception in log. Is there any way to get the message of SQLException and display in Toast etc. I tried to use try catch to catch the exception and show message but when exception occurs it doesnt go to catch block. Below is my code
public void insertData(SQLiteDatabase sqLiteDatabase, Context context, ContentValues values, DatabaseOperation dbOperation, DatabaseOperationCallback dbOperationCallback) {
JSONObject resultObj = new JSONObject();
try {
if (dbOperation != null) {
sqLiteDatabase.insert(dbOperation.getTableName(), null, values);
}
}
} catch (SQLException ex) {
ex.printStackTrace();
//showing in toast; this is not triggered
} catch (Exception ex) {
ex.printStackTrace();
//showing in toast; this is not triggered
}
}
Please see below picture for reference. Any help will be appreciated
Upvotes: 1
Views: 97
Reputation: 56948
You can use
e.printStackTrace(); // (directly to the log)
or
e.getMessage(); // returns message as a String
or others as per the Java Exception Class.
However, using the insertOrThrow method, is probably the method that you want to use.
Upvotes: 1