简体   繁体   中英

Android sqlite existing database not used

I'm playing around with Sqlite but it seems like the database is not found....it actually blames the table but I dont believe that.

 Error: android.database.sqlite.SQLiteException: no such table: food (code 1): , while compiling:  SELECT * FROM food

Here is my database: /data/data/com.haze.purple.foodorcars/databases/foods.db I have double checked this with rootExplorer amd the file is there. I have also downloaded this db file in windows and tried an sqlite editor and I can find the table food....so it must be the code.

So I believe that it's the file that somehow is not found. Here is my DatabaseHelper:

 public CarsOrFoodDatabaseHelper(Context appContext) {
    super(appContext, DATABASE_FILENAME, null, DB_VERS);
    this.context = appContext;
    DATABASE_FILENAME = context.getResources().getString(R.string.databasefile);
 }

Should I use context or applicationContext for my dbhelper?

Can I print somewhere the actual file and path to db that is used in my dbhelper?(stupid question maybe:)

The problem in above code is you are setting DATABASE_FILENAME to some other value after calling super(...) method. Make sure that your DATABASE_FILENAME will be initialized outside the constructor or you declare it as static final string in the same class.

Your code should look like as follows.

static final String DATABASE_FILENAME="foods";

public CarsOrFoodDatabaseHelper(Context appContext) {
    super(appContext, DATABASE_FILENAME, null, DB_VERS);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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