gpt4 book ai didi

java - Android SQLite 列......不是唯一的

转载 作者:搜寻专家 更新时间:2023-11-01 09:01:10 24 4
gpt4 key购买 nike

我在 android 上使用 SQLite 并收到此错误

02-11 18:05:37.224: E/SQLiteDatabase(26583): android.database.sqlite.SQLiteConstraintException: columns name, foreign_word are not unique (code 19)

但是,这是在调用 onUpgrade() 之后立即调用的,因此数据库本身应该没有任何内容。这是使用的 SQL 语句:

private static final String CREATE_DB_TB_2 = "CREATE TABLE "
+ TABLE_VOCAB_WORDS + "(" + COLUMN_NAME + " TEXT NOT NULL,"
+ COLUMN_WORD_FOREIGN + " TEXT NOT NULL," + COLUMN_WORD_ENGLISH
+ " TEXT NOT NULL," + COLUMN_CORRECT
+ " BYTE NOT NULL, PRIMARY KEY (" + COLUMN_NAME + ","
+ COLUMN_WORD_FOREIGN + "))";

由于删除表时数据库中没有任何内容,为什么添加内容时出现错误?此外,我正在调用 context.deleteDatabase(NAME);每次我运行应用程序。由于表是空的,所以事物必须是唯一的!

提前致谢

完整的 LogCat:

02-11 18:38:02.811: E/SQLiteDatabase(28631): Error inserting correct=0 english_meaning= tener prisa foreign_word=to be in a hurry  name=AQA GCSE Spanish Higher
02-11 18:38:02.811: E/SQLiteDatabase(28631): android.database.sqlite.SQLiteConstraintException: columns name, foreign_word are not unique (code 19)
02-11 18:38:02.811: E/SQLiteDatabase(28631): at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
02-11 18:38:02.811: E/SQLiteDatabase(28631): at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:775)
02-11 18:38:02.811: E/SQLiteDatabase(28631): at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
02-11 18:38:02.811: E/SQLiteDatabase(28631): at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
02-11 18:38:02.811: E/SQLiteDatabase(28631): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1525)
02-11 18:38:02.811: E/SQLiteDatabase(28631): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1395)
02-11 18:38:02.811: E/SQLiteDatabase(28631): at com.sharma.gcsevocabtester.DatabaseManipulator.addWord(DatabaseManipulator.java:168)
02-11 18:38:02.811: E/SQLiteDatabase(28631): at com.sharma.gcsevocabtester.DatabaseManipulator$ImportVocabList.onPostExecute(DatabaseManipulator.java:420)
02-11 18:38:02.811: E/SQLiteDatabase(28631): at com.sharma.gcsevocabtester.DatabaseManipulator$ImportVocabList.onPostExecute(DatabaseManipulator.java:1)
02-11 18:38:02.811: E/SQLiteDatabase(28631): at android.os.AsyncTask.finish(AsyncTask.java:631)
02-11 18:38:02.811: E/SQLiteDatabase(28631): at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-11 18:38:02.811: E/SQLiteDatabase(28631): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-11 18:38:02.811: E/SQLiteDatabase(28631): at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 18:38:02.811: E/SQLiteDatabase(28631): at android.os.Looper.loop(Looper.java:155)
02-11 18:38:02.811: E/SQLiteDatabase(28631): at android.app.ActivityThread.main(ActivityThread.java:5454)
02-11 18:38:02.811: E/SQLiteDatabase(28631): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 18:38:02.811: E/SQLiteDatabase(28631): at java.lang.reflect.Method.invoke(Method.java:511)
02-11 18:38:02.811: E/SQLiteDatabase(28631): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
02-11 18:38:02.811: E/SQLiteDatabase(28631): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
02-11 18:38:02.811: E/SQLiteDatabase(28631): at dalvik.system.NativeStart.main(Native Method)

此外,我不确定这是否会产生影响,但我还有另一个使用此字符串创建的表。

private static final String CREATE_DB_TB_1 = "CREATE TABLE "
+ TABLE_VOCAB_INFO + "(" + COLUMN_NAME
+ " TEXT NOT NULL PRIMARY KEY," + COLUMN_URL + " TEXT NOT NULL,"
+ COLUMN_DOWNLOADED + " TEXT NOT NULL)";

ImportVocabList 是一个异步任务。这是 onPostExecute :

**
* Method called after execution has happen, passing in the result
*
* @Override
*/
protected void onPostExecute(ArrayList<Word> result) {
super.onPostExecute(result);
progressDialog.dismiss();
boolean b = false;
open();
for (Word w : result) {

addWord(w, vlName);
}
if (getAllVocabListsWhere(DatabaseManipulator.COLUMN_DOWNLOADED,
"false").size() == 0) {
b = true;
}
close();
if (b) {
context.finish();
}
}

addWord方法如下:

          public boolean addWord(Word w, String name) {
try {
ContentValues cv = new ContentValues();
cv.put(COLUMN_NAME, name);
cv.put(COLUMN_WORD_FOREIGN, w.getWordForeign());
cv.put(COLUMN_WORD_ENGLISH, w.getWordEnglish());
cv.put(COLUMN_CORRECT, w.getCorrect());
database.insert(TABLE_VOCAB_WORDS, null, cv);
return true;
} catch (SQLiteException e) {
System.out.println("SQL ERROR");
return false;
}
}

问题已解决 - 存在重复条目但已被忽略。谢谢

最佳答案

查看您的 AsyncTask 的 onPostExecute() 方法,我相信您在 result 中至少有一组重复的值。只需从您的来源中清除这些重复项即可。

或者你可以 tell your table忽略具有重复条目的行并通过 "conflict-clause" 继续:

private static final String CREATE_DB_TB_2 = "CREATE TABLE "
+ TABLE_VOCAB_WORDS + "(" + COLUMN_NAME + " TEXT NOT NULL,"
+ COLUMN_WORD_FOREIGN + " TEXT NOT NULL," + COLUMN_WORD_ENGLISH
+ " TEXT NOT NULL," + COLUMN_CORRECT
+ " BYTE NOT NULL, PRIMARY KEY (" + COLUMN_NAME + ","
+ COLUMN_WORD_FOREIGN + ") ON CONFLICT IGNORE)";

关于java - Android SQLite 列......不是唯一的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14818246/

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