There are various way in populating a database. What I do is I create an insert(ObjectType objectName)
in the DBAdapter Class. That being said, I create an object class and for this example, I'm going to use Authorized Personnel
public class AuthorizedPersonnelClass {
private String _id;
private String Last_Name;
private String Middle_Name;
private String First_Name;
private String Store_ID;
private String Status;
private String New_Personnel;
//of course insert your 2 constructors and getter setter methods here
}
In my DBAdapter, I'll create the insert(AuthorizedPersonnelClass authorizedPersonnel)
method to handle the data insertions:
public long addPersonnel(AuthorizedPersonnelClass authorizedPersonnel){
ContentValues values = new ContentValues();
values.put(AUTHORIZEDPERSONNEL_ID, authorizedPersonnel.get_id());
values.put(L_NAME_AUTHORIZED_PERSONNEL, authorizedPersonnel.getLast_Name());
values.put(M_NAME_AUTHORIZED_PERSONNEL, authorizedPersonnel.getMiddle_Name());
values.put(F_NAME_AUTHORIZED_PERSONNEL, authorizedPersonnel.getFirst_Name());
values.put(STATUS, authorizedPersonnel.getStatus());
values.put(STORE_ID, authorizedPersonnel.getStore_ID());
values.put(NEW, authorizedPersonnel.getNew_Personnel());
return this.mDB.insert(TABLE_AUTHORIZED_PERSONNEL, null, values);
}
And from there, let's say I want to populate entries in my onCreate()
function or in a button call, I'll just do as such:
//instantiate a global variable for the DBAdapter
DBAdapter db = new DBAdapter(this);
//then if you want to insert
db.insert(new AuthorizedPersonnelClass( /*insert variables here*/ ));
Of course these values may be hard coded or user input (just use EditTexts and extract the Strings and use them there).
Here, I used the ContentValues
example because it's easier for beginners to use as opposed to doing a rawQuery Insert statement which may get confusing.