mardi 2 février 2016

Create a table in Android with few rows of my own

I am trying to create a local database on an android phone using sqlite with few rows that will already be added on table creation. I also would like to allow users to add rows to this table

I am able to successfully create a table using the OnCreate method as shown below.

` @Override

public void onCreate(SQLiteDatabase database) {
    database.execSQL("CREATE TABLE tv (_id INTEGER PRIMARY KEY AUTOINCREMENT, brand_name TEXT, model_name TEXT, price TEXT);");
}

`

I am also able to allow users to add rows to this table. But, I am not sure of how to add few rows of my own on table creation. I have tied to add rows of my own to this table from the MainActivity's On create method, but the problem I am encountering is that these rows gets added several times(each time the main activity's on create is called) and I am ending up with duplicate rows.

My requirement is to add these rows only on table creation or on DB upgrade. How can I accomplish this?

Aucun commentaire:

Enregistrer un commentaire