dimanche 4 octobre 2015

SQLiteAssetHelper - 'No such table' error

I have prepopulated a database with DB Browser for SQLite and tried to retrieve data from it using SQLiteAssetHelper (installed and used it using the guide at http://ift.tt/1dBxfAL ) and got a "No such table" error. I am sure that there is a table named that way in the database. I've tried to debug it and saw that I leave the DATABASE_VERSION as equal to one than it detects that there is already a database and doesn't copy it, though if I change the database version and setForceUpdate to (true) then it copies it, but I get the same error anyway. What should I do for SQLiteAssetHelper to copy my database properly and get rid of that annoying error?

Code:

DataBaseHelper class:

public class DataBaseHelper extends SQLiteAssetHelper {

private static final String DATABASE_NAME = "DonRoll";
private static final int DATABASE_VERSION = 2;

private static DataBaseHelper instance;

public static DataBaseHelper getInstance(Context context){
    if (null == instance){
        instance = new DataBaseHelper(context);
    }
    return instance;
}

private DataBaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

public Cursor getAllCategoryNames() {
    setForcedUpgrade(2);
    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    qb.setTables("MenuCategories");
    Cursor c = qb.query(db, null, null, null, null, null, null);
    c.moveToFirst();
    return c;
}

}

Activity class:

public class CategoryListActivity extends ListActivity {

DataBaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_category_list);
    db = DataBaseHelper.getInstance(this);
    getData();
}

@SuppressWarnings("deprecation")
private void getData() {
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
            android.R.layout.simple_list_item_1,
            db.getAllCategoryNames(),
            new String[] { "value" },
            new int[] { android.R.id.text1 });

    ListView listView = (ListView) findViewById(android.R.id.list);
    listView.setAdapter(adapter);
}

}

Aucun commentaire:

Enregistrer un commentaire