Reputation: 3651
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();
}
}
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);
}
}
Upvotes: 1
Views: 58
Reputation: 1007434
On the whole, if you are using SQLiteAssetHelper
(or even SQLiteOpenHelper
), use it consistently. Don't mix and match using the helper and accessing the database separately (e.g., via 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. 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. Typically, the helper turns into a singleton or is wrapped in a ContentProvider
, so you can get to it from everywhere that's needed.
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.
Upvotes: 2