简体   繁体   English

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

[英]Insert example data only on creation of Sqlite database

I need to put some example data into a Sqlite database when my android app is run for the for the first time. 第一次运行android应用时,我需要将一些示例数据放入Sqlite数据库。

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. 我的第一个想法是在onCreate中调用我的createSampleData(),但随后该数据库将被递归调用,并且应用程序崩溃。

In my onCreate all my tables are create. 在我的onCreate中,所有表均已创建。 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) { 公共无效onCreate(SQLiteDatabase数据库){

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

Insert the data in database helper onCreate() using the SQLiteDatabase parameter passed in as an argument. 使用作为参数传入的SQLiteDatabase参数将数据插入数据库助手onCreate()中。

Do not attempt to call getWritableDatabase() in onCreate() or a method called from there - that would cause the "called recursively" exception you've seen. 不要尝试在onCreate()调用getWritableDatabase()或从那里调用的方法-这会导致您看到的“递归调用”异常。

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()? ) 我了解到onCreate仅在首次创建表时被调用(第二个答案: Android-我必须检查表是否存在于SqliteHelper.onCreate()吗?

So I just put my sample data at the end of my onCreate method. 因此,我只是将示例数据放在onCreate方法的末尾。 Also worth noting is that you can not run multiple SQL stastements in one execSQL call. 另外值得注意的是,您不能在一个execSQL调用中运行多个SQL站点。 Otherwise I would have combined the INSERTs with the CREATE TABLE statements. 否则,我将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)");   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM