di SQLiteOpenHelper ada metode yang dipanggil sekali dan hanya sekali, yaitu onCreate metode. jadi jika Anda perlu menambahkan data ke DB hanya satu kali, Anda harus melakukannya di dalam metode ini.
Tetapi jika karena alasan tertentu Anda ingin menambahkan lebih banyak data ke DB, Anda dapat melakukannya di onUpgrade metode kali ini.
ambil contoh sederhana ini:
public class DatabaseHandler extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "dbName";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "tableName";
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
//this function called only once ever in the life of the app
@Override
public void onCreate(SQLiteDatabase database) {
//Create database query
database.execSQL("create table " + TABLE_NAME + " (column1 type, columun2 type...); ");
//Insert query
database.execSQL("insert into " + TABLE_NAME + " values(value1,value2...);");
database.execSQL("insert into " + TABLE_NAME + " values(value1,value2...);");
database.execSQL("insert into " + TABLE_NAME + " values(value1,value2...);");
database.execSQL("insert into " + TABLE_NAME + " values(value1,value2...);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//add more insert query if you need to add more datas after, but you have first to upgrade your DATABASE_VERSION to a higher number
}
}