1

In my app, I am using SQLite database. While inserting data in one of my tables, I am facing weird issue of "SQLite exception: error 19". For the first time data gets inserted but, when I try to insert data again, it throws exception.

My create table query:

"CREATE TABLE Tickets ("
            + "ID integer PRIMARY KEY NOT NULL,"
            + " UID varchar(40) NOT NULL DEFAULT 'not available'," + " OrderID int NOT NULL,"
            + " CarrierTicketNum varchar(15) NOT NULL,"
            + " TicketTypeID int NOT NULL," + " TankTypeID int NULL,"
            + " ProductObsGravity numeric NULL DEFAULT 0,"
            + " ProductHighTemp numeric NULL DEFAULT 0,"
            + " ProductLowTemp numeric NULL DEFAULT 0,"
            + " ProductObsTemp numeric NULL DEFAULT 0,"
            + " ProductBSW numeric NULL DEFAULT 0");

Insert Query:

public void insertTank(int OrderID, int TicketTypeID,
        String CarrierTicketNum, String TankNum, int TankTypeID,
        Double ProductObsTemp, Double ProductObsGravity, Double ProductBSW,
        Double GrossBarrels, Double NetBarrels) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("OrderID", OrderID);
    values.put("CarrierTicketNum", CarrierTicketNum);
    values.put("TankNum", TankNum);
    values.put("TicketTypeID", TicketTypeID);
    values.put("TankTypeID", TankTypeID);
    values.put("ProductObsTemp", ProductObsTemp);
    values.put("ProductObsGravity", ProductObsGravity);
    values.put("ProductBSW", ProductBSW);
    values.put("GrossBarrels", GrossBarrels);
    values.put("NetBarrels", NetBarrels);
    //try {
        db.insertOrThrow("Tickets", null, values);
    /*} catch (SQLiteException sqle) {
        Log.d(TAG, "SQLITException: " + sqle.getMessage());
    }*/
    db.close(); // Closing database connection
}

Following is the log detail:

09-08 13:54:57.610: E/AndroidRuntime(5713): FATAL EXCEPTION: main
09-08 13:54:57.610: E/AndroidRuntime(5713): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
09-08 13:54:57.610: E/AndroidRuntime(5713):     at android.database.sqlite.SQLiteStatement.native_executeInsert(Native Method)
09-08 13:54:57.610: E/AndroidRuntime(5713):     at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:113)
09-08 13:54:57.610: E/AndroidRuntime(5713):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1718)
09-08 13:54:57.610: E/AndroidRuntime(5713):     at android.database.sqlite.SQLiteDatabase.insertOrThrow(SQLiteDatabase.java:1617)
09-08 13:54:57.610: E/AndroidRuntime(5713):     at com.dzo.dispatchcrude.driverapp.datalayer.DatabaseHandler.insertTank(DatabaseHandler.java:736)
09-08 13:54:57.610: E/AndroidRuntime(5713):     at com.dzo.dispatchcrude.driverapp.NetBarrelActivity$1.onClick(NetBarrelActivity.java:103)
09-08 13:54:57.610: E/AndroidRuntime(5713):     at android.view.View.performClick(View.java:3511)
09-08 13:54:57.610: E/AndroidRuntime(5713):     at android.view.View$PerformClick.run(View.java:14110)
09-08 13:54:57.610: E/AndroidRuntime(5713):     at android.os.Handler.handleCallback(Handler.java:605)
09-08 13:54:57.610: E/AndroidRuntime(5713):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-08 13:54:57.610: E/AndroidRuntime(5713):     at android.os.Looper.loop(Looper.java:137)
09-08 13:54:57.610: E/AndroidRuntime(5713):     at android.app.ActivityThread.main(ActivityThread.java:4424)
09-08 13:54:57.610: E/AndroidRuntime(5713):     at java.lang.reflect.Method.invokeNative(Native Method)
09-08 13:54:57.610: E/AndroidRuntime(5713):     at java.lang.reflect.Method.invoke(Method.java:511)
09-08 13:54:57.610: E/AndroidRuntime(5713):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-08 13:54:57.610: E/AndroidRuntime(5713):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-08 13:54:57.610: E/AndroidRuntime(5713):     at 
dalvik.system.NativeStart.main(Native Method)

Read almost number posts but didn't got much help.

9
  • so u want to update data already exist? Commented Sep 8, 2013 at 8:55
  • No, I want to insert new data every time. Is there anything wrong in syntax? Commented Sep 8, 2013 at 8:57
  • So you simply need to insert a new row? Commented Sep 8, 2013 at 8:58
  • Yes, I want to insert new row Commented Sep 8, 2013 at 9:02
  • 1
    Put a breakpoint on the insert statement. Inspect values. Does it contain any nulls? Commented Sep 8, 2013 at 9:06

3 Answers 3

9

There are 2 ways to resolve your issue.

First:

Declare PRIMARY KEY as autoincrement

Second:

When trying to insert a new row, provide different/unique ID for the new row.

To test it, please DELETE DATABASE before running again your app.

SOLUTION: This line should be:

+ "ID integer PRIMARY KEY AUTOINCREMENT," 

instead of

+ "ID integer PRIMARY KEY NOT NULL,"
Sign up to request clarification or add additional context in comments.

9 Comments

In sqlite, AutoIncrement for primary key is not required, right? Although, I tried with it but didn't worked.
Do you need to put NULL? Maybe, that could be the problem.
If you put NOT NULL you can't put not null values, but if you put nothing you can put even null values or not null values.
But your error is not this. You error is probably that you're trying to insert two times value in the same row (so same ID). Try do delete your database and re-run app with autoincrement for ID. It should work
I tried, as you said. I deleted the database, and rerun my app with autoincrement. And the app crashed on second insert.
|
1
android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed

You are getting constraint failed because you violated the constraint put on the field, like passing a null value into a column that you declare as not null when you create your table.

I think "ID integer PRIMARY KEY NOT NULL," this is causing you the problem, Please make sure it is autoincrement and the other values are not null as you put the Not null constraint.

4 Comments

But, I am not passing anything in Primary key. Also I tried with auto increment.
But one of the field UID(please refer create table query) will be updated from server while syncing, I cannot put anything to it. Can it be cause of problem?
Check the data you are inserting, and make sure two records do not have the same key.
I don't see any problem other than two records with same key here.
1

if you are using ID as int Value than

"ID integer PRIMARY KEY AUTOINCREMENT"

instead of

"ID integer PRIMARY KEY NOT NULL"

if you are using ID as String Value than

use below line to insert value db.insertWithOnConflict(TABLE_NAME, null,values,SQLiteDatabase.CONFLICT_IGNORE);

(Where values is ovject of ContentValues object)

instead of

db.insert(TABLE_NAME, null, values);

keep coding....smile...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.