masmic
masmic

Reputation: 3564

How to do SQL Database ID?

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.

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.

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.

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.

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.

This is the DatabaseHelper`s onCreate():

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE_NAME + " (" +
            Travelers._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
            Travelers.NAME + " TEXT NOT NULL " +
            ");");
}

Upvotes: 0

Views: 75

Answers (1)

AC-OpenSource
AC-OpenSource

Reputation: 357

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.

Upvotes: 1

Related Questions