1

I have an android application that is supposed to read and expand a DataBase that is already created on SQLite...it works fine on emulator by putting database in data/data/(packagename)/database folder on the file explorer of emulator.

Now problem is occuring with the real device. Obviously it doesnt have the DataBase to open.I tried to put DataBase in assets folder but I am not getting to open it with the openhelper. how to access my database file form assets folder.will you please give me the right solution because i am new in Android and java .

this is my dbheperclass .

public class FoodDbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "pkfood_calories.DB";
private static final int DATABASE_VERSION = 1;
private static final String CREATE_QUERY =
        "CREATE TABLE "+ Food.NewDishInfo.TABLE_NAME+"("
                + Food.NewDishInfo.DISH_NAME+" TEXT NOT NULL,"
                + Food.NewDishInfo.DISH_QUANTITY+" TEXT NOT NULL,"
                + Food.NewDishInfo.DISH_CALORIE+" INTEGER,"
                + Food.NewDishInfo.DISH_FAT+" TEXT NOT NULL,"
                + Food.NewDishInfo.DISH_PROTEIN+" TEXT NOT NULL,"
                + Food.NewDishInfo.DISH_SUGAR+" TEXT NOT NULL,"
                + Food.NewDishInfo.DISH_VITAMINS+" TEXT NOT NULL);";
public FoodDbHelper(Context context)
{
    super(context,DATABASE_NAME,null,DATABASE_VERSION);
    Log.e("DATABASE OPERATION","Database created / opened...");
}
@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_QUERY);
    Log.e("DATABASE OPERATION","Table created...");


}
public void addInformations(String name ,String quantity, Integer calorie, String fat ,
                            String protein,String sugar,String vitamins, SQLiteDatabase db){


    ContentValues contentValues = new ContentValues();
    contentValues.put(Food.NewDishInfo.DISH_NAME,name);
    contentValues.put(Food.NewDishInfo.DISH_QUANTITY,quantity);
    contentValues.put(Food.NewDishInfo.DISH_CALORIE,calorie);
    contentValues.put(Food.NewDishInfo.DISH_FAT,fat);
    contentValues.put(Food.NewDishInfo.DISH_PROTEIN,protein);
    contentValues.put(Food.NewDishInfo.DISH_SUGAR,sugar);
    contentValues.put(Food.NewDishInfo.DISH_VITAMINS,vitamins);

    db.insert(Food.NewDishInfo.TABLE_NAME, null, contentValues);
    Log.e("DATABASE OPERATION","one row inserted...");
}
public Cursor getInformations(SQLiteDatabase db){
    Cursor cursor;
    String[] projections = {Food.NewDishInfo.DISH_NAME,Food.NewDishInfo.DISH_QUANTITY,
            Food.NewDishInfo.DISH_CALORIE,Food.NewDishInfo.DISH_FAT,Food.NewDishInfo.DISH_PROTEIN,
            Food.NewDishInfo.DISH_SUGAR, Food.NewDishInfo.DISH_VITAMINS};
    cursor= db.query(Food.NewDishInfo.TABLE_NAME,projections,null,null,null,null,"DISH_NAME ASC");
    return cursor;
}

please help me to short out from this problem..

Bannings
  • 10,376
  • 7
  • 44
  • 54
faisal iqbal
  • 724
  • 1
  • 8
  • 20
  • visit http://stackoverflow.com/questions/2605555/android-accessing-assets-folder-sqlite-database-file-with-sqlite-extension – Bharatesh Jun 17 '15 at 05:32

2 Answers2

0

First thing is your logic of reading from asset folder is correct.

But writing to assets folder may not be a better logic.

The asset folder is part of the APK

To read a file from asset folder

context.getAssets().open("pkfood_calories.DB")

Assuming that you placed that file in assets folder

You can get the internal memory location using

context.getFilesDir()

Refer creating file in internal memory Android create folders in Internal Memory

Community
  • 1
  • 1
Dickens A S
  • 3,824
  • 2
  • 22
  • 45
0

You cannot write anything to an APK. So, your db is in assert folder which can only be used for reading purpose.

You need to migrate that db file in external or internal memory(according to requirement) before doing some writing part on it.

Suhail Mehta
  • 5,514
  • 2
  • 23
  • 37