I\'m developing an expense tracker where I want to populate the DB with a few records when the application first start. I tried to call the method in my splash activity and add
private final String SAMPLE_DB_NAME = "TheftData";
private final String SAMPLE_TABLE_NAME = "UserDetails";
String CreateTable = "CREATE TABLE IF NOT EXISTS " +
SAMPLE_TABLE_NAME +
" (Username VARCHAR, Password VARCHAR," +
" );";
Then in onCreate
protected void onCreate(Bundle savedInstanceState) {
try {
sampleDB = this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);
sampleDB.execSQL(CreateTable);
sampleDB.close();
}
catch(Exception e)
{Toast.makeText(getApplicationContext(), " Error: "+e.getMessage(), Toast.LENGTH_LONG).show();
sampleDB.close();
}
and to add data
public void insertData(String username,String password)
{
sampleDB = this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);
sampleDB.execSQL(CreateTable);
sampleDB.execSQL("INSERT INTO " +
SAMPLE_TABLE_NAME +
" Values ('"+username+"','"+ password+"');");
sampleDB.close();
}