你必須檢查:
- 目錄存在,如果不創建它。
- 文件存在,如果沒有創建它。
但是,如果你再實際上是試圖建立一個SQLite數據庫有這樣做
private static class OpenHelper extends SQLiteOpenHelper {
OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME
+ "(_id INTEGER PRIMARY KEY, name TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
while (oldVersion != newVersion) {
Log.w("Example",
"Upgrading database, this will drop tables and recreate.");
if (oldVersion == 4) {
db.execSQL("ALTER TABLE " + TABLE_NAME+ " ADD COLUMN type INTEGER");
}
else if(oldVersion>=5)
{
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
oldVersion++;
}
}
}
更好的方法