3

SQLite SQLite constraint conflict. Try / Catch could not catch

 2 years ago
source link: https://www.codesd.com/item/sqlite-sqlite-constraint-conflict-try-catch-could-not-catch.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

SQLite SQLite constraint conflict. Try / Catch could not catch

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


You need to use a android.database.sqlite.SQLiteConstraintException instead of a android.database.sqlite.SQLiteException

If this doesn't work, try and catch any exception with Exception to see if the catch block is being called.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK