android.database.sqlite.SQLiteConstraintException

时间:2010-11-22 13:20:24

标签: android

  

android.database.sqlite.SQLiteConstraintException:   错误代码19:约束失败

我的应用程序出错并且不确定原因。

当我向我的数据库添加新的收件箱项时,会发生这种情况。

这是我的收件箱表的Create语句:

CREATE TABLE inbox (uid text, title text, sender text, timeRecieved text, PRIMARY KEY(uid));

使用这些静态最终字符串实现;

public static final String LABEL_INBOX_UID = "uid";
    public static final String LABEL_INBOX_TITLE = "title";
    public static final String LABEL_INBOX_SENDER = "sender";
    public static final String LABEL_INBOX_TIME_RECIEVED = "timeRecieved";

    public static final int COLUMN_INBOX_UID = 0;
    public static final int COLUMN_INBOX_TITLE  = 1;
    public static final int COLUMN_INBOX_SENDER= 2;
    public static final int COLUMN_INBOX_TIME_RECIEVED = 3;

    public static final String TABLE_NAME_INBOX = "inbox";
    public static final String TABLE_CREATE_INBOX = "CREATE TABLE "
        + TABLE_NAME_INBOX + " (" + LABEL_INBOX_UID + " text, "
        + LABEL_INBOX_TITLE + " text, " + LABEL_INBOX_SENDER
        + " text, " + LABEL_INBOX_TIME_RECIEVED + " text, "
     + "PRIMARY KEY(" + LABEL_INBOX_UID + ")" + ");";

以下是获取收件箱项并将其放入contentValues()

的方法
@Override
    protected void insertItem(InboxListItem object, String table) {
        InboxListItem item = object;
        inboxItemValue = new ContentValues();

        inboxItemValue.put(LABEL_INBOX_UID, item.getUid()); 
        Log.d(TAG, "item.getUid() " + item.getUid());
        inboxItemValue.put(LABEL_INBOX_TIME_RECIEVED, item.getTimeRecieved());
        Log.d(TAG, "item.getTimeRecieved() " + item.getTimeRecieved());
        inboxItemValue.put(LABEL_INBOX_TITLE, item.getTitle());
        Log.d(TAG, "item.getTitle() " + item.getTitle());
        inboxItemValue.put(LABEL_INBOX_SENDER, item.getSender());
        Log.d(TAG, "item.getSender() " + item.getSender());


        Log.d(TAG, "TABLE_CREATE_EVENTS =" + TABLE_CREATE_EVENTS);
        Log.d(TAG, "TABLE_CREATE_INBOX =" + TABLE_CREATE_INBOX);
        Log.d(TAG, "TABLE_CREATE_CONTACTS =" + TABLE_CREATE_CONTACTS);
        database().insert(TABLE_NAME_INBOX, null, inboxItemValue);
        Log.d(TAG, "database().insert(TABLE_NAME_INBOX, null, inboxItemValue);");


    }

O,这是完整的异常消息:

  

11-22 12:24:39.606:   错误/数据库(773):插入错误   sender = Mit Technology Review   UID = 663b074a-16c8-46fb-A420-e0ebf716e212   title =新兴技术周五   更新(11/19/2010)   timeRecieved = 2010-11-19T06:02:36.000Z   11-22 12:24:39.606:   ERROR /数据库(773):   android.database.sqlite.SQLiteConstraintException:   错误代码19:约束失败11-22   12:24:39.606:错误/数据库(773):
  在   android.database.sqlite.SQLiteStatement.native_execute(母语   方法)11-22 12:24:39.606:   错误/数据库(773):at   android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:55)   11-22 12:24:39.606:   错误/数据库(773):at   android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1549)   11-22 12:24:39.606:   错误/数据库(773):at   android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410)   11-22 12:24:39.606:   错误/数据库(773):at   com.kc.database.InboxDbWrapper.insertItem(InboxDbWrapper.java:44)   11-22 12:24:39.606:   错误/数据库(773):at   com.kc.database.InboxDbWrapper.insertItem(InboxDbWrapper.java:1)   11-22 12:24:39.606:   错误/数据库(773):at   com.kc.database.DbWrapper.insertAllItemsToDb(DbWrapper.java:42)   11-22 12:24:39.606:   错误/数据库(773):at   com.kc.database.InboxDbWrapper.populateTable(InboxDbWrapper.java:54)   11-22 12:24:39.606:   错误/数据库(773):at   com.kc.main.screen.HomeMainScreenOne.loadDataToDb(HomeMainScreenOne.java:225)   11-22 12:24:39.606:   错误/数据库(773):at   com.kc.main.screen.AbstractMainScreen.onCreate(AbstractMainScreen.java:125)   11-22 12:24:39.606:   错误/数据库(773):at   com.kc.main.screen.HomeMainScreenOne.onCreate(HomeMainScreenOne.java:51)   11-22 12:24:39.606:   错误/数据库(773):at   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)   11-22 12:24:39.606:   错误/数据库(773):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)   11-22 12:24:39.606:   错误/数据库(773):at   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)   11-22 12:24:39.606:   错误/数据库(773):at   android.app.ActivityThread.access $ 2300(ActivityThread.java:125)   11-22 12:24:39.606:   错误/数据库(773):at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)   11-22 12:24:39.606:   错误/数据库(773):at   android.os.Handler.dispatchMessage(Handler.java:99)   11-22 12:24:39.606:   错误/数据库(773):at   android.os.Looper.loop(Looper.java:123)   11-22 12:24:39.606:   错误/数据库(773):at   android.app.ActivityThread.main(ActivityThread.java:4627)   11-22 12:24:39.606:   错误/数据库(773):at   java.lang.reflect.Method.invokeNative(母语   方法)11-22 12:24:39.606:   错误/数据库(773):at   java.lang.reflect.Method.invoke(Method.java:521)   11-22 12:24:39.606:   错误/数据库(773):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)   11-22 12:24:39.606:   错误/数据库(773):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)   11-22 12:24:39.606:   错误/数据库(773):at   dalvik.system.NativeStart.main(母语   方法)

提前致谢:

编辑:更多信息。我以前创建了一个事件和contactsList表成功,这似乎工作正常。这是下面的代码:

联系人:

// Variables used to create the events table
    public static final String TABLE_NAME_CONTACTS = "contactsList";

    public static final String LABEL_CONTACTS_UID = "uid";
    public static final String LABEL_CONTACTS_FULLNAME = "fullname";

    public static final String TABLE_CREATE_CONTACTS = "CREATE TABLE "
            + TABLE_NAME_CONTACTS + " (" + LABEL_CONTACTS_UID + " text, "
            + LABEL_CONTACTS_FULLNAME + " text, " + "PRIMARY KEY("
            + LABEL_CONTACTS_UID + ")" + ");";


    public static final int COLUMN_CONTACTS_UID = 0;
public static final int COLUMN_CONTACTS_FULLNAME = 1;

CREATE TABLE contactsList (uid text, fullname text, PRIMARY KEY(uid));

日历活动:

public static final String LABEL_EVENTS_UID = "uid";
    public static final String LABEL_EVENTS_TITLE = "title";
    public static final String LABEL_EVENTS_LOCATION = "location";
    public static final String LABEL_EVENTS_DATE = "date";
    public static final String LABEL_EVENTS_TIME = "time";
    public static final String LABEL_EVENTS_TIMEZONE = "timezone";

    public static final int COLUMN_EVENTS_UID = 0;
    public static final int COLUMN_EVENTS_TITLE = 1;
    public static final int COLUMN_EVENTS_LOCATION = 2;
    public static final int COLUMN_EVENTS_DATE = 3;
    public static final int COLUMN_EVENTS_TIME = 4;
    public static final int COLUMN_EVENTS_TIMEZONE = 5;

    // Variables used to create the events table
    public static final String TABLE_NAME_EVENTS = "events";
    public static final String TABLE_CREATE_EVENTS = "CREATE TABLE "
            + TABLE_NAME_EVENTS + " (" + LABEL_EVENTS_UID + " text, "
            + LABEL_EVENTS_TITLE + " varchar(30), " + LABEL_EVENTS_LOCATION
            + " varchar(30), " + LABEL_EVENTS_DATE + " varchar(30), "
            + LABEL_EVENTS_TIME + " varchar(30), " + LABEL_EVENTS_TIMEZONE
            + " varchar(30), " + "PRIMARY KEY(" + LABEL_EVENTS_UID + ")" + ");";

CREATE TABLE events (uid text, title varchar(30), location varchar(30), date varchar(30), time varchar(30), timezone varchar(30), PRIMARY KEY(uid));

4 个答案:

答案 0 :(得分:8)

您桌子上的唯一约束是主键,因此您可能尝试插入具有非唯一“UID”的项目。

答案 1 :(得分:3)

您的主键不是自动增量,因此每次在表中插入记录时,您都应负责生成唯一的键。您可以通过以下方式更改此行为:

CREATE TABLE inbox (uid integer PRIMARY KEY AUTOINCREMENT, title text, sender text, timeRecieved text);

答案 2 :(得分:1)

@jonney正如您所说,您已添加了新的表格内容。或者如果你更新你的db版本会发生同样的错误,但是不用担心,你做到了Buddy有效的原因是主键不是自动增量但是解决方案很简单只需卸载应用程序并重新安装它,我的一个。弗兰德告诉我这个,它的确有效。 希望你的问题能得到解决。 祝你好运。

答案 3 :(得分:0)

试试这个:

return db.insertWithOnConflict(YOURTABLENAME, null, initialValues, SQLiteDatabase.CONFLICT_IGNORE);