TKV
TKV

Reputation: 2663

android sqliteDatabase table creation with unique key constraints not throwing exception for duplicate entry

i have created one android application that was connecting to sqlite database.

and i have given a unique constraint for multiple fields exactly 2 fields say username and password.

am expecting the app will thrown exception while am trying to insert the duplicate entry.

and in the Logcat am getting the following exception :

01-04 16:52:06.604: E/SQLiteDatabase(1432): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed

in the line am trying to insert db.insert(userTable, null, cv);

but even if am writing this code in a try..catch block the control is not entered into my catch block. Experts please tell why this case happends.?

Thanks in advance.

Upvotes: 3

Views: 1739

Answers (3)

Niranjan
Niranjan

Reputation: 1909

you can try this:

try{
     db.insertOrThrow(userTable, null, cv);
} catch (android.database.sqlite.SQLiteConstraintException ex) {

}

Upvotes: 0

waqaslam
waqaslam

Reputation: 68187

use as below:

try{
    db.insertOrThrow(userTable, null, cv);
}
catch(SQLiteConstraintException ex){
//what ever you want to do
}

Upvotes: 2

clemp6r
clemp6r

Reputation: 3723

Try catching SQLException instead of SQLiteConstraintException

Upvotes: 0

Related Questions