简体   繁体   English

无法访问预加载的SQLite数据库文件

[英]Unable to access preloaded SQLite database file

I am using a pre loaded SQLite database for my android app. 我正在为我的Android应用程序使用预加载的SQLite数据库。 It is working fine when I test it on an emulator. 当我在模拟器上测试时,它工作正常。 But when I try it on a real device, it returns error saying: 但是,当我在真实设备上尝试使用它时,它会返回错误消息:

android.database.sqlite.SQLiteException: no such table: supplier android.database.sqlite.SQLiteException:没有这样的表:Supplier

I have checked that database file and it definitely has the table 'supplier'. 我已经检查了该数据库文件,并且肯定有表'supplier'。

I believe I have placed the .db file in the correct location (Initially misspelled and I got errors that DB not found. I fixed that and the error is gone since). 我相信我已经将.db文件放置在正确的位置(最初拼写错误,并且出现了未找到DB的错误。我已修复该错误,此后错误消失了)。

Using following code and dependency. 使用以下代码和依赖项。 Have attached a screen shot as to where the database is located now. 随附了有关数据库现在所在位置的屏幕截图。 Please advice. 请指教。

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);
    }
}

在此处输入图片说明

On the whole, if you are using SQLiteAssetHelper (or even SQLiteOpenHelper ), use it consistently. 总体而言,如果您使用的是SQLiteAssetHelper (甚至SQLiteOpenHelper ),请始终使用它。 Don't mix and match using the helper and accessing the database separately (eg, via openOrCreateDatabase() ). 不要使用帮助器来混合和匹配,也不要单独访问数据库(例如,通过openOrCreateDatabase() )。 In part, that is because you want there to be only one instance of the SQLiteDatabase for this database at a time, shared by all threads, for synchronization purposes. 在某种程度上,这是因为您希望该数据库一次只存在一个 SQLiteDatabase实例,该实例被所有线程共享以用于同步目的。 That's easiest if you have one consistent spot of getting the SQLiteDatabase , and if you're using a helper, that helper should be the consistent spot. 如果您拥有一个一致的获取SQLiteDatabase ,那么这是最容易的,如果您使用的是一个帮助程序,则该帮助程序应该是一致的位置。 Typically, the helper turns into a singleton or is wrapped in a ContentProvider , so you can get to it from everywhere that's needed. 通常,帮助程序变成单例或包装在ContentProvider ,因此您可以从所需的任何地方访问它。

That being said, I can't quite explain your former symptoms, as I would have expected it to work. 话虽如此,我无法完全解释您以前的症状,因为我希望它能起作用。 Though, since I use openOrCreateDatabase() approximately once a year, I don't have a ton of experience with your specific scenario. 但是,由于我大约每年使用openOrCreateDatabase()一次,因此对于您的特定情况我没有很多经验。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM