我正在扩展DaoMaster.OpenHelper来管理资产文件夹数据库。由于它不是用户相关的数据,因此变化可能非常大,我想要实现的唯一一件事就是删除并在有新版本时重新创建数据库。我不明白的是,当我在模拟器上调试我的应用程序时,或者即使我生成apk并手动将其安装在我的设备上,此代码也能正常工作。但是当我在市场上部署我的应用程序并将其从我那里安装到我的设备上时,看起来它无法找到数据库。
异常java.lang.RuntimeException:无法恢复活动{com.livos.companionplants / com.livos.companionplants.main.MainActivity}:android.database.sqlite.SQLiteException:no such column:T.definition(code 1 ):,同时编译:SELECT T。“_ id”,T。“plant_id”,T。“definition”FROM“plant_definition”T ##################### #############################################错误代码:1(SQLITE_ERROR )引起:SQL(查询)错误或缺少数据库。 (没有这样的列:T.definition(代码1):,同时编译:SELECT T。“_ id”,T。“plant_id”,T。“definition”FROM“plant_definition”T)######### ################################################## ######
@ApplicationScope
public class DatabaseOpenHelper extends DaoMaster.OpenHelper {
private String TAG = DatabaseOpenHelper.class.getSimpleName();
private static final String SP_KEY_DB_VER = "db_ver";
private static final int DATABASE_VERSION = 1;
private Context context;
private SharedPreferences sharedPreferences;
private SQLiteDatabase sqliteDatabase;
private static String DB_PATH;
private static String DB_NAME;
public DatabaseOpenHelper(Context context, SharedPreferences sharedPreferences, String name, SQLiteDatabase.CursorFactory factory) {
super(context, name, factory);
this.context = context;
this.sharedPreferences = sharedPreferences;
DB_NAME = name;
DB_PATH = context.getFilesDir().getAbsolutePath().replace("files", "databases") + File.separator;
try {
createDataBase();
} catch (Exception ioe) {
throw new Error("Unable to create database");
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
/** Open Database for Use */
public void openDatabase() {
String databasePath = DB_PATH + DB_NAME;
sqliteDatabase = SQLiteDatabase.openDatabase(databasePath, null,
(SQLiteDatabase.OPEN_READWRITE));
}
/** Close Database after use */
@Override
public synchronized void close() {
if ((sqliteDatabase != null) && sqliteDatabase.isOpen()) {
sqliteDatabase.close();
}
super.close();
}
/** Get database instance for use */
public SQLiteDatabase getSqliteDatabase() {
return sqliteDatabase;
}
/** Create new database if not present */
public void createDataBase() {
SQLiteDatabase sqliteDatabase;
if (databaseExists()) {
int dbVersion = sharedPreferences.getInt(SP_KEY_DB_VER, 1);
// If different version then delete current database and copy the new one from assets
if (DATABASE_VERSION != dbVersion) {
File dbFile = context.getDatabasePath(DB_NAME);
boolean dbFileDeleted = dbFile.delete();
if (!dbFileDeleted) {
Log.w(TAG, "Unable to update database");
} else {
createDataBase();
}
}
/* Check for Upgrade */
} else {
/* Database does not exists create blank database */
sqliteDatabase = this.getReadableDatabase();
sqliteDatabase.close();
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(SP_KEY_DB_VER, DATABASE_VERSION);
editor.apply();
copyDataBase();
}
}
/** Check Database if it exists */
private boolean databaseExists() {
SQLiteDatabase sqliteDatabase = null;
try {
String databasePath = DB_PATH + DB_NAME;
sqliteDatabase = SQLiteDatabase.openDatabase(databasePath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
e.printStackTrace();
}
if (sqliteDatabase != null) {
sqliteDatabase.close();
}
return sqliteDatabase != null;
}
/**
* Copy existing database file in system
*/
public void copyDataBase() {
int length;
byte[] buffer = new byte[1024];
String databasePath = DB_PATH + DB_NAME;
try {
InputStream databaseInputFile = this.context.getAssets().open(DB_NAME);
OutputStream databaseOutputFile = new FileOutputStream(databasePath);
while ((length = databaseInputFile.read(buffer)) > 0) {
databaseOutputFile.write(buffer, 0, length);
databaseOutputFile.flush();
}
databaseInputFile.close();
databaseOutputFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static boolean isRoboUnitTest() {
return "robolectric".equals(Build.FINGERPRINT);
}
答案 0 :(得分:0)
使用Kuffs命题的解决方案
<a href="{!! route('models.destroy', $model->id) !!}" class="btn btn-danger>Confirm</a>