1

在我的应用程序中,我使用的是 SQLite 数据库。在我的一个表中插入数据时,我遇到了“SQLite 异常:错误 19”的奇怪问题。第一次插入数据,但是当我再次尝试插入数据时,它会抛出异常。

我的创建表查询:

"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");

插入查询:

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
}

以下是日志详细信息:

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)

几乎阅读了数字帖子,但没有得到太多帮助。

4

3 回答 3

9

有两种方法可以解决您的问题。

第一的:

声明PRIMARY KEYautoincrement

第二:

尝试插入新行时,provide different/unique ID为新行。

要对其进行测试,请DELETE DATABASE在再次运行您的应用程序之前进行。

解决方案: 这条线应该是:

+ "ID integer PRIMARY KEY AUTOINCREMENT," 

代替

+ "ID integer PRIMARY KEY NOT NULL,"
于 2013-09-08T09:02:47.320 回答
1
android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed

你得到约束失败,因为你违反了放在字段上的约束,比如将一个空值传递到你在创建表时声明为非空的列。

我认为"ID integer PRIMARY KEY NOT NULL,"这会导致您出现问题,请确保它是autoincrement并且其他值不为空,因为您放置了 Not null 约束。

于 2013-09-08T09:09:06.907 回答
1

如果您使用 ID 作为 int 值

"ID 整数 PRIMARY KEY AUTOINCREMENT"

代替

“ID 整数 PRIMARY KEY NOT NULL”

如果您使用 ID 作为字符串值

使用下面的行插入值 db.insertWithOnConflict(TABLE_NAME, null,values,SQLiteDatabase.CONFLICT_IGNORE);

(其中 values 是 ContentValues 对象的对象)

代替

db.insert(TABLE_NAME, null, values);

继续编码……微笑……

于 2016-07-20T18:26:31.900 回答