gpt4 book ai didi

android.database.sqlite.SQLiteConstraintException

转载 作者:太空宇宙 更新时间:2023-11-03 11:41:27 26 4
gpt4 key购买 nike

android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed

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

当我将一个新的收件箱项目添加到我的数据库中时会发生这种情况。

这是我的收件箱表的创建语句:

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: ERROR/Database(773): Error inserting sender=Mit Technology Review uid=663b074a-16c8-46fb-a420-e0ebf716e212 title=Emerging Technologies Friday Update (11/19/2010) timeRecieved=2010-11-19T06:02:36.000Z 11-22 12:24:39.606: ERROR/Database(773): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed 11-22 12:24:39.606: ERROR/Database(773):
at android.database.sqlite.SQLiteStatement.native_execute(Native Method) 11-22 12:24:39.606: ERROR/Database(773): at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:55) 11-22 12:24:39.606: ERROR/Database(773): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1549) 11-22 12:24:39.606: ERROR/Database(773): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410) 11-22 12:24:39.606: ERROR/Database(773): at com.kc.database.InboxDbWrapper.insertItem(InboxDbWrapper.java:44) 11-22 12:24:39.606: ERROR/Database(773): at com.kc.database.InboxDbWrapper.insertItem(InboxDbWrapper.java:1) 11-22 12:24:39.606: ERROR/Database(773): at com.kc.database.DbWrapper.insertAllItemsToDb(DbWrapper.java:42) 11-22 12:24:39.606: ERROR/Database(773): at com.kc.database.InboxDbWrapper.populateTable(InboxDbWrapper.java:54) 11-22 12:24:39.606: ERROR/Database(773): at com.kc.main.screen.HomeMainScreenOne.loadDataToDb(HomeMainScreenOne.java:225) 11-22 12:24:39.606: ERROR/Database(773): at com.kc.main.screen.AbstractMainScreen.onCreate(AbstractMainScreen.java:125) 11-22 12:24:39.606: ERROR/Database(773): at com.kc.main.screen.HomeMainScreenOne.onCreate(HomeMainScreenOne.java:51) 11-22 12:24:39.606: ERROR/Database(773): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 11-22 12:24:39.606: ERROR/Database(773): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 11-22 12:24:39.606: ERROR/Database(773): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 11-22 12:24:39.606: ERROR/Database(773): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 11-22 12:24:39.606: ERROR/Database(773): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 11-22 12:24:39.606: ERROR/Database(773): at android.os.Handler.dispatchMessage(Handler.java:99) 11-22 12:24:39.606: ERROR/Database(773): at android.os.Looper.loop(Looper.java:123) 11-22 12:24:39.606: ERROR/Database(773): at android.app.ActivityThread.main(ActivityThread.java:4627) 11-22 12:24:39.606: ERROR/Database(773): at java.lang.reflect.Method.invokeNative(Native Method) 11-22 12:24:39.606: ERROR/Database(773): at java.lang.reflect.Method.invoke(Method.java:521) 11-22 12:24:39.606: ERROR/Database(773): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 11-22 12:24:39.606: ERROR/Database(773): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 11-22 12:24:39.606: ERROR/Database(773): at dalvik.system.NativeStart.main(Native Method)

提前致谢:

编辑:更多信息。我之前已经成功创建了一个 events 和 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));

最佳答案

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

关于android.database.sqlite.SQLiteConstraintException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4245795/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com