我试过网络寻找一个似乎逃避我的解决方案。
我将不胜感激。
课程如下:
public class DBAdapter extends SQLiteOpenHelper {
// DBPATH uses the default system path for a given application
// /data/data/<app namespace> , which in our example will be
// com.wordpress.arogyavidya.mooligaimarmam
private static String DBPATH = "/data/data/com.wordpress.arogyavidya.mooligaimarmam/databases/";
private static String DBNAME = "mooligaimarmam.db";
// private static String DATABASE_TABLE = "lkgchef";
// static final String KEY_ROWID = "_id";
// static final String KEY_NAME = "name";
// static final String KEY_DETAIL = "desc";
private SQLiteDatabase myDatabase;
private final Context myContext;
// constructor
public DBAdapter(Context context) {
super(context, DBNAME, null, 3);
this.myContext = context;
//DBPATH = this.myContext.getDatabasePath("mooligaimarmam.db").getPath();
}
// create an empty db, and replace with our chosen db
public void createDatabase() throws IOException {
if (!checkDatabase()) {
this.getWritableDatabase();
try {
copyDatabase();
} catch (IOException e) {
throw new Error("Error copying database from system assets");
}
}
}
// Check if our database already exists
private boolean checkDatabase() {
SQLiteDatabase checkableDatabase = null;
try {
checkableDatabase = SQLiteDatabase.openDatabase(DBPATH+DBNAME,
null, SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
// our database doesn't exist, so we'll return false below.
}
if (checkableDatabase != null) {
checkableDatabase.close();
}
return checkableDatabase != null ? true : false;
}
// Copy our database from the Application's assets over the empty DB for use
private void copyDatabase() throws IOException {
InputStream myInput = myContext.getAssets().open(DBNAME);
OutputStream myOutput = new FileOutputStream(DBPATH+DBNAME);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDatabase() throws SQLException {
myDatabase = SQLiteDatabase.openDatabase(DBPATH + DBNAME, null,
SQLiteDatabase.OPEN_READWRITE);
/*myDatabase = SQLiteDatabase.openDatabase(DBPATH, null,
SQLiteDatabase.OPEN_READWRITE);*/
}
@Override
public synchronized void close() {
if (myDatabase != null)
myDatabase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
// Handle creation tasks, etc.
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Handle upgrade tasks, etc.
myDatabase.close();
myContext.deleteDatabase(DBPATH+DBNAME);
try {
this.createDatabase();
} catch (IOException e) {
throw new Error("Error copying database from system assets");
}
}
}
感谢。
答案 0 :(得分:0)
尝试一次!不要删除数据库,只需删除表。这对我来说每次都适用。
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_Name);
onCreate(db);
}