i have a list with some items.The use can save some of them in a database,as favorites.My problem is that i m getting duplicated records in the database if he try to add the same item two times in the favorites.How can i fix it?
我有一些项目的清单。这种用法可以将其中一些保存到数据库中作为收藏。我的问题是,如果他试图在收藏夹中两次添加相同的条目,那么我将在数据库中得到重复的记录。我怎样才能修好它呢?
i was thinking something like
我想的是
ourDatabase.rawQuery("DELETE FROM "+DBHelper.DATABASE_TABLE+" WHERE"+DBHelper.NAME+" EQUALS "+DBHelper.NAME+");",null);
but it's not working.
但这不是工作。
This is my create Entry method
这是我的create Entry方法
public void createEntry(String name, String dodes) {
try {
ContentValues cv = new ContentValues();
cv.put(NAME, name);
cv.put(DODES, dodes);
// ourDatabase.rawQuery("DELETE FROM "+DBHelper.DATABASE_TABLE+" WHERE "+DBHelper.NAME+" = "+DBHelper.NAME+");",null);
ourDatabase.insert("DB", null, cv);
} catch (Exception e) {
Log.e("Exception in insert :", e.toString());
e.printStackTrace();
}
}
2 个解决方案
#1
0
Before inserting, check to see if an item with the same name already exists. If so, do an update instead of an insert.
在插入之前,检查具有相同名称的项是否已经存在。如果是的话,做一个更新而不是插入。
Cursor crsr = null;
try
{
crsr = ourDatabase.rawQuery("SELECT _id FROM "+DBHelper.DATABASE_TABLE
+" WHERE "+DBHelper.NAME+"='"+name+"'", null);
}
catch ( Exception excp )
{
crsr = null;
}
if ( (crsr == null) || (crsr.getCount() < 1) )
// Insert item
else
// Update item
#2
3
The simple answer is to ensure that the record is "unique".
简单的答案是确保记录是“唯一的”。
You can do this inside the database by adding a "unique index".
您可以在数据库中添加“惟一索引”来实现这一点。
We want something like
我们希望类似的
CREATE UNIQUE INDEX unique_name ON some_table(name)
So something like this in code;
代码中有这样的东西;
"CREATE UNIQUE INDEX unique_name ON "+
DBHelper.DATABASE_TABLE+
"("+DBHelper.NAME+")"
This means that if you try and insert a duplicate, you will get an error.
这意味着如果您尝试插入一个副本,您将会得到一个错误。
Therefore you'll also need to add code that checks whether the name
already exists, before you try and do it.
因此,您还需要添加代码来检查名称是否已经存在,然后再尝试执行它。
#1
0
Before inserting, check to see if an item with the same name already exists. If so, do an update instead of an insert.
在插入之前,检查具有相同名称的项是否已经存在。如果是的话,做一个更新而不是插入。
Cursor crsr = null;
try
{
crsr = ourDatabase.rawQuery("SELECT _id FROM "+DBHelper.DATABASE_TABLE
+" WHERE "+DBHelper.NAME+"='"+name+"'", null);
}
catch ( Exception excp )
{
crsr = null;
}
if ( (crsr == null) || (crsr.getCount() < 1) )
// Insert item
else
// Update item
#2
3
The simple answer is to ensure that the record is "unique".
简单的答案是确保记录是“唯一的”。
You can do this inside the database by adding a "unique index".
您可以在数据库中添加“惟一索引”来实现这一点。
We want something like
我们希望类似的
CREATE UNIQUE INDEX unique_name ON some_table(name)
So something like this in code;
代码中有这样的东西;
"CREATE UNIQUE INDEX unique_name ON "+
DBHelper.DATABASE_TABLE+
"("+DBHelper.NAME+")"
This means that if you try and insert a duplicate, you will get an error.
这意味着如果您尝试插入一个副本,您将会得到一个错误。
Therefore you'll also need to add code that checks whether the name
already exists, before you try and do it.
因此,您还需要添加代码来检查名称是否已经存在,然后再尝试执行它。