SQLiteOpenHelper不会触发onCreate

时间:2013-05-21 16:16:28

标签: android

我正在使用此代码

public class PeopleDBHelper extends SQLiteOpenHelper {
public static final String TABLE_NAME = "Person";
public static final String DB_PATH = "/data/data/com.machinarius.labs.preloadedsql/databases/";
public static final String DB_NAME = "preload-test.sqlite";

private static final String DB_PREFS = "whyDoIHaveToDoThisAgain?";

private Context parent;

public PeopleDBHelper(Context context) {
    super(context, DB_NAME, null, 1);

    parent = context;
    SharedPreferences prefs = context.getSharedPreferences(DB_PREFS, 0);
    if(prefs.getBoolean("firstRun", true)) {
        prefs.edit().putBoolean("firstRun", false).commit();
        onCreate(null);
    }
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    Log.i(getClass().getSimpleName(), "onCreate()");
    InputStream source;
    OutputStream destination;
    byte[] buffer;

    try {
        source = parent.getAssets().open(DB_NAME);
        destination = new BufferedOutputStream(new FileOutputStream(DB_PATH + DB_NAME));
        buffer = new byte[512];

        while(source.read(buffer) > 0) {
            destination.write(buffer);
        }

        source.close();
        destination.flush();
        destination.close();
    } catch(IOException ex) {
        Log.e(getClass().getSimpleName(), "IOException", ex);
    }
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {

    }
}

我一直想知道......为什么我必须用SharedPreferences实现那个愚蠢的黑客攻击才能让onCreate解雇?即使在从“设置”菜单中擦除应用程序数据并且调用getWritableDatabase()onCreate也不会触发,导致来自持久层的崩溃。我做错了什么?

0 个答案:

没有答案