我正在爲我的android應用程序使用預加載的SQLite數據庫。當我在模擬器上測試時它工作正常。但是,當我嘗試在真實設備上,它會返回錯誤說:無法訪問預加載的SQLite數據庫文件
android.database.sqlite.SQLiteException:沒有這樣的表:供應商
我已經檢查了數據庫文件,它肯定有表'供應商'。
我相信我已經把.db文件在正確的位置(最初拼寫錯誤,我得到的錯誤,DB沒有找到。我固定的,並且由於錯誤消失)。
使用以下代碼和依賴關係。附上了一個屏幕快照,瞭解數據庫現在的位置。請指教。
dependencies {
compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:+'
}
。
String tableName = "supplier";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//getting the database stored in assets folder
try {
DBHelper dbHelper = new DBHelper(this);
dbHelper.getReadableDatabase();
} catch (SQLiteAssetHelper.SQLiteAssetException e) {
e.printStackTrace();
}
}
public void sqlTest(View v){
SQLiteDatabase db = this.openOrCreateDatabase("QuoteDb", MODE_PRIVATE, null);
String[] inserts = {category, preferences.getString(category, "1")};
//this line throws the table name not found error
Cursor c = db.rawQuery("SELECT quote FROM " + tableName + " WHERE category=? AND rowid=? LIMIT 1", inserts);
}
private class DBHelper extends SQLiteAssetHelper{
private static final String DATABASE_NAME = "QuoteDb.db";
private static final int DATABASE_VERSION = 1;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
}
「我已經檢查了數據庫文件,它肯定有表‘供應商’」 - 在設備或模擬器上,或在您的項目?您可能想要卸載或「清除數據」您的應用程序,然後再次嘗試安裝您的應用程序。使用'DATABASE_VERSION = 1',如果在該設備或模擬器上已經有數據庫,'SQLiteAssetHelper'不會執行任何操作,因爲它認爲您已經解壓數據庫。如果那裏的數據庫碰巧沒有這個表格,那會解釋你的症狀。 – CommonsWare
@CommonsWare通過「有表」,我的意思是我檢查了sqliteBrowser上的文件,確保它在粘貼到資產/數據庫文件夾之前具有必要的表和字段。 – kar
然後我支持我的建議:卸載或「清除數據」應用程序,所以你確定'SQLiteAssetHelper'將解壓當前位於'assets /'中的文件,而不是使用一些缺少你的表的文件。你可能也會拋棄'openOrCreateDatabase()'並一直使用'SQLiteAssetHelper'。 – CommonsWare