Question

I wrote this code:

// CurrentFriendsTable
// ---------------------
//| _id | email | uname |
// ---------------------

// FFriend class
class FFriend {
    String email, uname;
    // getter and setter
}

// add single friend to database
public long addFriend(FFriend friend) {
    ContentValues values = new ContentValues();
    values.put(CurrentFriendsTable.COLUMN_EMAIL, friend.getEmail());
    values.put(CurrentFriendsTable.COLUMN_UNAME, friend.getName());
    return database.insert(CurrentFriendsTable.TABLE_NAME, null, values);
}

// add list of friends to database
public void addListOfFriends(List<FFriend> listOfFriend){
    int lenghOfList = listOfFriend.size();
    for (FFriend friend : listOfFriend)
        addFriend(friend);
}

I have two question here:

  1. when I add values to table using database.insert() function, is that adding the _id automatically ? (I defined the column as _id primary key autoincrement)

  2. is the scenario of calling addFriend(FFriend friend) function in addListOfFriends(List<FFriend> listOfFriend) good to use of may I change ?

Was it helpful?

Solution

1) The primary key is automatically added if you define your key as:

primary key autoincrement

2) I think it is good. (As suggested here this is a good way for looping)

Just a suggestion: Do not use internal getter and setter in Android. Use it only if you need to get or set the value from outside of your target class (reference)

OTHER TIPS

1) It is upto you to do auto increment or you can add primary Key manually. You can do this from the properties of your table in database.

2) In my opinion it is a good way to insert data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top