In the app I'm developing, the user can save 5 diferent names. Then, the user will select which names wants to use and they will be inserted into a list.
在我正在开发的应用程序中,用户可以保存5个不同的名称。然后,用户将选择要使用的名称,并将它们插入列表中。
To do this, I'm using 3 DB whose manage the entire functionality. In the first DB I save the names. Those names have an autogenerated ID in the DB. Then, when the user selects some names, those name's ID is saved in other DB, and then they loaded in the list and saved in the las DB.
为此,我使用3个DB来管理整个功能。在第一个DB中我保存了名称。这些名称在DB中具有自动生成的ID。然后,当用户选择某些名称时,这些名称的ID将保存在其他数据库中,然后将它们加载到列表中并保存在las DB中。
The problem I'm facing is that, when I edit a name, lets say the one with ID 3, it remains having that ID, so when I go again to the list, I check for the update and it changes the old name with the new one, checking the matching ID.
我面临的问题是,当我编辑名称时,让我们说一个ID为3的名称,它仍然具有该ID,所以当我再次进入列表时,我检查更新并更改旧名称新的,检查匹配的ID。
But If I delete a name and create a new one, then it will have a new ID value, lets say 7, and when I want to replace it in the list, it won't find the old one, because this new ID does not exist.
但是,如果我删除一个名称并创建一个新名称,那么它将有一个新的ID值,比方说7,当我想在列表中替换它时,它将找不到旧名称,因为这个新ID确实如此不存在。
I don't have much experience with Databases, and I don't know if I could for example manage the ID value manually... I'm a bit stucked so I would appreciate any suggestion about how to face this.
我没有太多的数据库经验,我不知道我是否可以手动管理ID值...我有点被困,所以我很感激有关如何面对这个的任何建议。
This is the DatabaseHelper`s onCreate():
这是DatabaseHelper的onCreate():
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " (" +
Travelers._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
Travelers.NAME + " TEXT NOT NULL " +
");");
}
1 个解决方案
#1
When you delete a name you should also delete it from your other databases. Then just insert a new name and also insert that to the other databases.
删除名称时,还应从其他数据库中删除该名称。然后只需插入一个新名称,并将其插入其他数据库。
Or, what would I do, is I would set a flag when a record is deleted so that I know that the record should be replaced with a new one so that you know what records to replace.
或者,我会做什么,是在删除记录时设置一个标志,以便我知道应该用新记录替换记录,以便您知道要替换哪些记录。
For example:
I added 5 names on Database1: ID Name 1 Hello 2 World 3 I 4 am 5 AC-OpenSource I also added this records on database 2: ID DB1ID is_deleted 2 1 //hello false 5 4 //am false 7 2 //world false 20 3 //I false 22 5 //AC-OpenSource false If I delete the record with ID 5 in Database1 I would also update is_deleted of Database2 with DB1ID of 5 to true You should figure out what to do next.
#1
When you delete a name you should also delete it from your other databases. Then just insert a new name and also insert that to the other databases.
删除名称时,还应从其他数据库中删除该名称。然后只需插入一个新名称,并将其插入其他数据库。
Or, what would I do, is I would set a flag when a record is deleted so that I know that the record should be replaced with a new one so that you know what records to replace.
或者,我会做什么,是在删除记录时设置一个标志,以便我知道应该用新记录替换记录,以便您知道要替换哪些记录。
For example:
I added 5 names on Database1: ID Name 1 Hello 2 World 3 I 4 am 5 AC-OpenSource I also added this records on database 2: ID DB1ID is_deleted 2 1 //hello false 5 4 //am false 7 2 //world false 20 3 //I false 22 5 //AC-OpenSource false If I delete the record with ID 5 in Database1 I would also update is_deleted of Database2 with DB1ID of 5 to true You should figure out what to do next.