0

I want to only insert like 10 rows into a table at database creation. I do not want them added every time the app is open. Just once as a table with final values. How would I go about doing this? Thanks!

public void insertEvents(){
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(COL_EVENT, "value1");
    values.put(COL_EVENT, "value2");
    values.put(COL_EVENT, "value3");
    values.put(COL_EVENT, "value4");

    db.insert(TABLE_EVENTS, null, values);
}
Joe Eigi
  • 1,041
  • 2
  • 8
  • 8

3 Answers3

1

Do it in onCreate method of DatabaseHelper

EDIT: here is the similar question. Or you can try this tutorial

Community
  • 1
  • 1
Georgiy Shur
  • 748
  • 1
  • 8
  • 19
1

Since you are using SQLiteOpenHelper, put the inserts in its onCreate(). Use the SQLiteDatabase passed in as argument, do not recursively call getWritableDatabase().

laalto
  • 150,114
  • 66
  • 286
  • 303
  • Got it, when I called getWritableDatabase() before I would get recursive error since I was calling it in the main activity as well – Joe Eigi Jun 14 '15 at 20:58
0

I know this might be too late but I'm just sharing my answers. Your code seems to show that you are trying to add 4 rows. Here are some pointers:

1) Do it in the onCreate method, as mentioned by @Georgiy Shur.

2) The insert method only inserts one row at a time. So you should do this instead:

ContentValues values = new ContentValues();
values.put(COL_EVENT, "value1");
db.insert(TABLE_EVENTS, null, values);
values.put(COL_EVENT, "value2");
db.insert(TABLE_EVENTS, null, values);
values.put(COL_EVENT, "value3");
db.insert(TABLE_EVENTS, null, values);
values.put(COL_EVENT, "value4");
db.insert(TABLE_EVENTS, null, values);
remykarem
  • 2,251
  • 22
  • 28