Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SQLite Contraint Conflict. Try/Catch couldn't catch

08-02 16:26:24.750: E/Database(28841): Error inserting data
08-02 16:26:24.750: E/Database(28841): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
08-02 16:26:24.750: E/Database(28841):  at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
08-02 16:26:24.750: E/Database(28841):  at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:61)
08-02 16:26:24.750: E/Database(28841):  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1584)
08-02 16:26:24.750: E/Database(28841):  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1428)
08-02 16:26:24.750: E/Database(28841):  at com.app.android.DataOperations.insertItems(DataOperations.java:69)
08-02 16:26:24.750: E/Database(28841):  at com.app.android.DownloadTask.createDatabase(DownloadTask.java:173)
08-02 16:26:24.750: E/Database(28841):  at com.app.android.DownloadTask.doInBackground(DownloadTask.java:59)
08-02 16:26:24.750: E/Database(28841):  at com.app.android.DownloadTask.doInBackground(DownloadTask.java:1)
08-02 16:26:24.750: E/Database(28841):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-02 16:26:24.750: E/Database(28841):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-02 16:26:24.750: E/Database(28841):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-02 16:26:24.750: E/Database(28841):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-02 16:26:24.750: E/Database(28841):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-02 16:26:24.750: E/Database(28841):  at java.lang.Thread.run(Thread.java:1019)

Above is my LogCat report. What I am trying to do is to insert data. After looking at the logcat I found that it is a constraint violation. I tried to put my code as:

public void insert(...){
            long insertID = 0;
            try{
                Log.i("DataOperation", String.valueOf(insertID));
                insertID = sqllitedb.insert(DBase.TABLE_NAME, null, contentValues);
            }catch(android.database.sqlite.SQLiteException ex){
                Log.i("dataops_", "Error Constraint"); //Print
            }

}

Nothing is happening.. My logcat is full of stacktraces. No Sign of the line with comment(see above).

How can it be handled. N.B. There is only one insert satements(which is above) in my whole program. Test Ok then I will proceed.

Also tell me how to handle whether my database is created/already exists so that if exists I will not run my DownloadTask.

like image 206
Debopam Mitra Avatar asked Dec 09 '22 22:12

Debopam Mitra


1 Answers

Maybe insert(..) method catch exceptions itself.

use insertOrThrow(..) method instead.

    try {
        SQLiteDatabase db = SQLiteHelper.getDatabase();
        db.insertOrThrow(Table, nullColumnHack, values);
    } catch (SQLiteConstraintException e) {
        Log.d(TAG, "failure to insert word,", e);
        return false;
    }
like image 185
Louis Huh Avatar answered Dec 11 '22 10:12

Louis Huh