how to create a sqlite database with data inside? I search for a tutorial for that but i saw only that they create only a table in database then they make a method for inserting a data into certain table. I want to make a database with a data itself upon creating a database.
Asked
Active
Viewed 166 times
-2
-
Step #1: Create the starter database on your development machine, using any number of SQLite client tools (`sqlite3`, DB Browser for SQLite, etc.). Step #2: Package and deploy the database with your app [using `SQLiteAssetHelper`](https://github.com/jgilfelt/android-sqlite-asset-helper). – CommonsWare Oct 14 '17 at 16:49
1 Answers
0
Creating a method to insert dummy or existing data is industry-practice, the following is a dummy method to insert data given the SQLiteDatabase object. Just call this kind of method only once upon database creation. For example of a database holding tasks:
private void loadDemoTask(SQLiteDatabase db) {
ContentValues values = new ContentValues();
values.put(TaskColumns.DESCRIPTION, mContext.getResources().getString(R.string.demo_task));
values.put(TaskColumns.DUE_DATE, Long.MAX_VALUE);
db.insertOrThrow(DatabaseContract.TABLE_TASKS, null, values);
}
In your dbHelper class (extending from SQLiteDbHelper), call the above method in onCreate. This way, the method will only be called once when the database table is created.
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_TABLE_TASKS);
loadDemoTask(db);
}

Andrew Lam
- 1,371
- 17
- 35