我在我的应用程序中使用预填充数据库,但是当我在预填充数据库中添加新行并增加数据库版本时,它不会复制新内容。要复制新内容,我必须重新安装该应用。
请帮我解决这个问题我希望在升级数据库时复制新行。
这是我的DBHelper.java
package com.suvariyaraj.algorithms.Database;
/**
* Created by GOODBOY-PC on 04/06/16.
*/
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
public class ExternalDbOpenHelper extends SQLiteOpenHelper {
//Path to the device folder with databases
public static String DB_PATH;
//Database file name
public static String DB_NAME;
public SQLiteDatabase database;
public final Context context;
public static final int DB_VERSION = 1;
public SQLiteDatabase getDb() {
return database;
}
public ExternalDbOpenHelper(Context context, String databaseName) {
super(context, databaseName, null, DB_VERSION);
this.context = context;
//Write a full path to the databases of your application
String packageName = context.getPackageName();
DB_PATH = String.format("//data//data//%s//databases//", packageName);
DB_NAME = databaseName;
openDataBase();
Log.d("mycheck", "constructor "+database.getVersion());
}
//This piece of code will create a database if it’s not yet created
public void createDataBase() {
boolean dbExist = checkDataBase();
if (!dbExist) {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
Log.e(this.getClass().toString(), "Copying error");
throw new Error("Error copying database!");
}
} else {
Log.i(this.getClass().toString(), "Database already exists");
}
}
//Performing a database existence check
private boolean checkDataBase() {
SQLiteDatabase checkDb = null;
try {
String path = DB_PATH + DB_NAME;
checkDb = SQLiteDatabase.openDatabase(path, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLException e) {
Log.e(this.getClass().toString(), "Error while ch1ecking db");
}
//Android doesn’t like resource leaks, everything should
// be closed
if (checkDb != null) {
checkDb.close();
}
return checkDb != null;
}
//Method for copying the database
private void copyDataBase() throws IOException {
//Open a stream for reading from our ready-made database
//The stream source is located in the assets
InputStream externalDbStream = context.getAssets().open(DB_NAME);
//Path to the created empty database on your Android device
String outFileName = DB_PATH + DB_NAME;
//Now create a stream for writing the database byte by byte
OutputStream localDbStream = new FileOutputStream(outFileName);
//Copying the database
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = externalDbStream.read(buffer)) > 0) {
localDbStream.write(buffer, 0, bytesRead);
}
//Don’t forget to close the streams
localDbStream.close();
externalDbStream.close();
}
public SQLiteDatabase openDataBase() throws SQLException {
String path = DB_PATH + DB_NAME;
if (database == null) {
createDataBase();
database = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE);
}
return database;
}
@Override
public synchronized void close() {
if (database != null) {
database.close();
}
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d("mycheck", "on create");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Toast.makeText(context,"Upgrade",Toast.LENGTH_SHORT).show();
Log.d("mycheck", "Upgrade");
context.deleteDatabase(DB_NAME);
openDataBase();
}
}
答案 0 :(得分:0)
那是因为您没有在onUpgrade方法中添加更改。该方法适用于修改/更新现有数据库。如果您在此处未执行任何操作,则反映更改的唯一方法是让用户重新安装该应用程序。
onUpgrade:
需要升级数据库时调用。实现应使用此方法删除表,添加表或执行升级到新架构版本所需的任何其他操作。
例如:
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < YOUR_NEW_DB_VERSION) {
db.execSQL(YOUR_QUERY_TO_ADD_NEW_DATA_TO_EXISTING_TABLE);
}
}
如果您在使用onUpgrade时需要更多帮助,请查看此post。希望这有帮助!