0

I need to put some example data into a Sqlite database when my android app is run for the for the first time.

My database works great and I have a working method that puts example data into the database. But my problem is where do I call it? I only want my sample data to be inserted when the database is first created. The user can then remove it

My first idea was to call my createSampleData() in onCreate, but then the db will be called recursively and the app crashes.

In my onCreate all my tables are create. Maybe I should put something in those statements? Should I somehow see if these tables get created and then insert the example data?

Fumbling here.

public void onCreate(SQLiteDatabase database) {

  String sessionTableQuery =
          "CREATE TABLE "
                  + SESSION_TABLE + "("
                  + "id INTEGER PRIMARY KEY AUTOINCREMENT,"
                  + "comment TEXT,"
                  + "date DATETIME DEFAULT CURRENT_TIMESTAMP)";

  String exerciseTableQuery =
          "CREATE TABLE "
                  + EXERCISE_TABLE + " ("
                  + "exercisId INTEGER PRIMARY KEY AUTOINCREMENT,"
                  + "name TEXT,"
                  + "weight INTEGER,"
                  + "reps INTEGER,"
                  + "rpm INTEGER,"
                  + "score FLOAT,"
                  + "sessionId INTEGER NOT NULL)";

   String customTimerSessionTableQuery =
          "CREATE TABLE "
                  + CUSTOM_TIMER_SESSION_TABLE + "("
                  + "id INTEGER PRIMARY KEY AUTOINCREMENT,"
                  + "name TEXT,"
                  + "rounds INTEGER,"
                  + "date DATETIME DEFAULT CURRENT_TIMESTAMP)";

  String customTimerTableQuery =
          "CREATE TABLE "
                  + CUSTOM_TIMER_TABLE + " ("
                  + "timerId INTEGER PRIMARY KEY AUTOINCREMENT,"
                  + "name TEXT,"
                  + "millis INTEGER,"
                  + "metronome BOOL,"
                  + "rpm INTEGER,"
                  + "type INTEGER,"
                  + "sessionId INTEGER NOT NULL)";

  database.execSQL(sessionTableQuery);
  database.execSQL(exerciseTableQuery);
  database.execSQL(customTimerSessionTableQuery);
  database.execSQL(customTimerTableQuery);    }
Community
  • 1
  • 1
Christoffer
  • 7,470
  • 9
  • 39
  • 55

2 Answers2

1

Insert the data in database helper onCreate() using the SQLiteDatabase parameter passed in as an argument.

Do not attempt to call getWritableDatabase() in onCreate() or a method called from there - that would cause the "called recursively" exception you've seen.

laalto
  • 150,114
  • 66
  • 286
  • 303
0

I solved it so I thought I'd add an answer to my own question.

I learned that the onCreate is only called when the table is first created (second answer: Android- Must I check if table exists in SqliteHelper.onCreate()?)

So I just put my sample data at the end of my onCreate method. Also worth noting is that you can not run multiple SQL stastements in one execSQL call. Otherwise I would have combined the INSERTs with the CREATE TABLE statements.

public void onCreate(SQLiteDatabase database) {

  [... code code code ...]

   String customTimerSessionTableQuery =
          "CREATE TABLE "
                  + CUSTOM_TIMER_SESSION_TABLE + "("
                  + "id INTEGER PRIMARY KEY AUTOINCREMENT,"
                  + "name TEXT,"
                  + "rounds INTEGER,"
                  + "date DATETIME DEFAULT CURRENT_TIMESTAMP)";

  String customTimerTableQuery =
          "CREATE TABLE "
                  + CUSTOM_TIMER_TABLE + " ("
                  + "timerId INTEGER PRIMARY KEY AUTOINCREMENT,"
                  + "name TEXT,"
                  + "millis INTEGER,"
                  + "metronome BOOL,"
                  + "rpm INTEGER,"
                  + "type INTEGER,"
                  + "sessionId INTEGER NOT NULL)";

  database.execSQL(sessionTableQuery);
  database.execSQL(exerciseTableQuery);
  database.execSQL(customTimerSessionTableQuery);
  database.execSQL(customTimerTableQuery);

  // SAMPLE DATA
  database.execSQL("INSERT INTO custom_timer_session VALUES(1,'Tabata',8,'2014-10-21 12:00:00')");
  database.execSQL("INSERT INTO custom_timer VALUES(1,'WORK',20000,0,20,0,1)");
  database.execSQL("INSERT INTO custom_timer VALUES(2,'REST',10000,0,20,1,1)");   }
Community
  • 1
  • 1
Christoffer
  • 7,470
  • 9
  • 39
  • 55