gpt4 book ai didi

java - 为什么我收到 'UNIQUE constraint failed' 错误

转载 作者:行者123 更新时间:2023-12-01 19:07:34 25 4
gpt4 key购买 nike

我尝试将数据保存到 MyList 但收到此错误:UNIQUE 约束失败。我的错在哪里?

我的数据库

db.execSQL("CREATE TABLE IF NOT EXISTS \"Words\" (\n" +
"\t\"Id\"\tINTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" +
"\t\"Name\"\tTEXT,\n" +
"\t\"Mean\"\tTEXT\n" +
");");
db.execSQL("CREATE TABLE IF NOT EXISTS \"MyList\" (\n" +
"\t\"Id\"\tINTEGER NOT NULL,\n" +
"\t\"Name\"\tTEXT,\n" +
"\t\"Mean\"\tTEXT,\n" +
"\tFOREIGN KEY(\"Id\") REFERENCES \"Words\"(\"Id\"),\n" +
"\tPRIMARY KEY(\"Id\")\n" +
");");

InsertWordToMyList(插入从 Words 中提取的单词)

ifExists(如果单词存在于我的列表中则不插入)

FindWord(我的主表是 Words,因此我在 Words 中搜索数据(如果存在),然后提取它)

public String InsertWordToList(DbConnection data,String kelime){


Word yeni=null;
SQLiteDatabase db= data.getWritableDatabase();

ContentValues values= new ContentValues();

String msg;
Boolean bool = ifExists(data,kelime,db);
if(bool){
msg= "kelime zaten var";
}
else {
yeni = FindWord(data, kelime, db);

if(yeni!=null){
Log.e("yeni id",Integer.toString(yeni.getId()));
values.put("Id", yeni.getId());
values.put("Name", yeni.getName());
values.put("Mean", yeni.getMean());


db.insertOrThrow("MyList", null, values);
msg="kelime eklendi";
}
else msg="kelime databasede kayırlı değil";



}
db.close();
return msg;

}


public Word FindWord(DbConnection data, String kelime, SQLiteDatabase db){

Word w= new Word();
//db= data.getWritableDatabase();



Cursor c = db.rawQuery("SELECT * FROM Words WHERE Name = 'kelime'",null);

if(c!=null){

while (c.moveToNext()){
w = new Word(c.getInt(c.getColumnIndex("Id"))
,c.getString(c.getColumnIndex("Name"))
,c.getString(c.getColumnIndex("Mean")));
}

return w;
}
else {

return null;
}
}
public Boolean ifExists (DbConnection data, String kelime, SQLiteDatabase db) {

Word w=null;
Cursor c = db.rawQuery("SELECT * FROM Mylist WHERE Name = 'kelime'",null);


while (c.moveToNext()){
w = new Word(c.getInt(c.getColumnIndex("Id"))
,c.getString(c.getColumnIndex("Name"))
,c.getString(c.getColumnIndex("Mean")));

}

if(w!=null) return true;
else return false;
}

InsertWordToMyList 的用法:

for(int i=0;i<get.size();i++){

msg=wordData.InsertWordToMyList(db,get.get(i));
}

最佳答案

我不知道这是否能解决您的问题,但因为您的代码正在搜索字符串文字 'kelime' 而不是字符串参数 kelime .
更改方法 FindWord()ifExists(),如下所示:

public Word FindWord(DbConnection data, String kelime, SQLiteDatabase db) {
Word w = null;
Cursor c = db.rawQuery("SELECT * FROM Words WHERE Name = ?", new String[] {kelime});
if (c.moveToNext()) {
w = new Word(c.getInt(c.getColumnIndex("Id"))
, c.getString(c.getColumnIndex("Name"))
, c.getString(c.getColumnIndex("Mean")));
}
c.close();
return w;
}

public Boolean ifExists(DbConnection data, String kelime, SQLiteDatabase db) {
Cursor c = db.rawQuery("SELECT * FROM Mylist WHERE Name = ?", new String[] {kelime});
int rows = c.getCount();
c.close();
return rows > 0;
}

我简化了大部分代码,因为您不需要循环。
我也没有看到哪里需要参数DbConnection

关于java - 为什么我收到 'UNIQUE constraint failed' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59522689/

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