mardi 5 janvier 2016

Unable to access preloaded SQLite database file

I am using a pre loaded SQLite database for my android app. 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

I have checked that database file and it definitely has the table '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).

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

void sqlTest(){
    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);
    }
 }

enter image description here

Aucun commentaire:

Enregistrer un commentaire