java数据库版本SQLite Android
我在学安卓。我有一个使用SQLite数据库的Quote应用程序。我需要给这个应用程序提供数据库版本,这样每当我更新应用程序时,用户的数据库也可以得到更新。我需要在这方面做些什么改变
我的数据库助手/处理程序类如下所示
public class DataBaseHandler extends SQLiteOpenHelper {
private static String DB_PATH;
private static String DB_NAME = "SuccessQuotesNew";
private SQLiteDatabase myDataBase;
private final Context myContext;
public DataBaseHandler(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
DB_PATH = context.getDatabasePath(DB_NAME).toString();
Log.e("path", DB_PATH);
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
} else {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
// ==============================================================================
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException {
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH;
OutputStream myOutput = new FileOutputStream(outFileName);
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 {
// Open the database
String myPath = DB_PATH;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
// ==============================================================================
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
// ==============================================================================
@Override
public void onCreate(SQLiteDatabase db) {
}
// ==============================================================================
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
# 1 楼答案
构造函数的最后一个参数是数据库的版本代码
super(context, DB_NAME, null, 1);
更改该数字并检查更新方法将被调用,所以当您想要更新数据库时,请更改它,一旦您创建了该对象,应用程序将调用更新方法参考请点击这里-Android SQLite database and app update