仅在创建Sqlite数据库时插入示例数据

时间:2014-10-21 12:54:09

标签: java android sql sqlite

当我的Android应用程序第一次运行时,我需要将一些示例数据放入Sqlite数据库。

我的数据库工作得很好,我有一个将示例数据放入数据库的工作方法。但我的问题是我在哪里称之为?我只希望在首次创建数据库时插入我的样本数据。然后用户可以将其删除

我的第一个想法是在onCreate中调用我的createSampleData(),但随后将递归调用db并且应用程序崩溃。

在我的onCreate中,我的所有表都是创建的。也许我应该在这些陈述中加入一些内容?我应该以某种方式查看是否创建了这些表,然后插入示例数据?

在这里摸索。

  

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);    }

2 个答案:

答案 0 :(得分:1)

使用作为参数传入的onCreate()参数将数据插入数据库助手SQLiteDatabase

不要试图在getWritableDatabase()中调用onCreate()或从那里调用的方法 - 这将导致"递归调用"你见过的例外。

答案 1 :(得分:0)

我解决了,所以我想我会为自己的问题添加一个答案。

我了解到onCreate仅在首次创建表时被调用(第二个答案:Android- Must I check if table exists in SqliteHelper.onCreate()?

所以我只是将我的示例数据放在onCreate方法的末尾。另外值得注意的是,您无法在一个execSQL调用中运行多个SQL装入。否则,我会将INSERT与CREATE TABLE语句结合起来。

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)");   }