我有一个以预先填充的数据库开头的应用程序。我想更新应用程序,插入新表并复制预先填充的新数据库。更新应用程序时,如果我将数据库的版本设置为 2,它会创建新表,但不会复制新数据库。但是如果我保持版本 1,应用程序会因错误而停止。
数据库位于 assets 文件夹中,并在第一次使用此代码进行复制:
public class DataBaseHelper<E> extends OrmLiteSqliteOpenHelper {
private static String DB_PATH = "/data/data/com.teste/databases/";
private static String DB_NAME = "teste.db";
private static int DB_VERSION = 1;
private SQLiteDatabase myDataBase;
Context context;
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.context = context;
try {
createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
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 + DB_NAME;
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 = context.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
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 {
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
}
@Override
public void onCreate(SQLiteDatabase db, ConnectionSource src) {
try {
TableUtils.createTable(src, Table1.class);
TableUtils.createTable(src, Table2.class);
TableUtils.createTable(src, Table3.class);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, ConnectionSource src,
int oldVersion, int newVersion) {
try {
TableUtils.dropTable(src, Table1.class, true);
TableUtils.dropTable(src, Table2.class, true);
TableUtils.dropTable(src, Table3.class, true);
onCreate(db, src);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
}
现在,我还有一张要插入的表格,我正在使用以下代码:
public class DataBaseHelper<E> extends OrmLiteSqliteOpenHelper {
private static String DB_PATH = "/data/data/com.teste/databases/";
private static String DB_NAME = "teste.db";
private static int DB_VERSION = 2;
private SQLiteDatabase myDataBase;
Context context;
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.context = context;
try {
createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
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 + DB_NAME;
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 = context.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
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 {
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
}
@Override
public void onCreate(SQLiteDatabase db, ConnectionSource src) {
try {
TableUtils.createTable(src, Table1.class);
TableUtils.createTable(src, Table2.class);
TableUtils.createTable(src, Table3.class);
TableUtils.createTable(src, Table4.class);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, ConnectionSource src,
int oldVersion, int newVersion) {
try {
TableUtils.dropTable(src, Table1.class, true);
TableUtils.dropTable(src, Table2.class, true);
TableUtils.dropTable(src, Table3.class, true);
TableUtils.dropTable(src, Table4.class, true);
onCreate(db, src);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
}
编辑2:
我在 onUpgrade 中调用 copyDatabase 并且没有复制数据库。看:
TableUtils.createTable(src, Linhas.class);
TableUtils.createTable(src, Horarios.class);
TableUtils.createTable(src, Itinerarios.class);
TableUtils.createTable(src, Utils.class);
copyDataBase();
有什么帮助吗?
编辑 3:
我注意到当我们使用该方法更改数据库升级版本并从资产文件夹复制数据库时,数据不会出现,因为该文件夹资产的数据库版本低于当前版本。有没有办法在预填充新数据库时复制,更改它的版本?