0

In my android application, I use SQLite database. It works only for emulator. When I'm installing my apk file into device, It won't show any data. But in my emulator it works very well. I can't understand about this problem.

1) Do I copy anything to device to make it work?

2) Is the database I created common for all?

3) Do I backup the database for every new user?

Someone explain please

public class SQLiteAdapterv {

public static final String MYDATABASE_NAME2 = "MY_DATABASE2";
public static final String MYDATABASE_TABLE2 = "MY_VLGMAS";
public static final int MYDATABASE_VERSION = 1;
public static final String KEY_ID = "_id";
public static final String KEY_CONTENT1 = "Content1";
public static final String KEY_CONTENT2 = "Content2";

public static final String KEY_CONTENT3 = "Content3";
public static final String KEY_CONTENT4 = "Content4";
public static final String KEY_VCODE = "vcode";
public static final String KEY_VNAME = "vname";
public static final String KEY_DIVSEC= "divsec";

private static final String SCRIPT_CREATE_DATABASE1 =
          "create table " + MYDATABASE_TABLE2 + " ("
          + KEY_ID + " integer primary key autoincrement, "
          + KEY_VCODE + " text not null, "
          + KEY_VNAME + " text not null,"
          + KEY_DIVSEC + " text not null);";

private SQLiteHelper sqLiteHelper;
private  SQLiteDatabase sqLiteDatabase;
private Context context;

public SQLiteAdapterv(Context c)
{
    context=c;
}
public SQLiteAdapterv openToRead() throws android.database.SQLException {
      sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME2, null, MYDATABASE_VERSION);
      sqLiteDatabase = sqLiteHelper.getReadableDatabase();
      return this; 
     }
public SQLiteAdapterv openToWrite() throws android.database.SQLException {
      sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME2, null, MYDATABASE_VERSION);
      sqLiteDatabase = sqLiteHelper.getWritableDatabase();
      return this; 
     }
public void close(){
      sqLiteHelper.close();
     }
public long insert(String vcode, String vname,String divsec){

      ContentValues contentValues = new ContentValues();
      contentValues.put(KEY_VCODE, vcode);
      contentValues.put(KEY_VNAME, vname);
      contentValues.put(KEY_DIVSEC, divsec);
      return sqLiteDatabase.insert(MYDATABASE_TABLE2, null, contentValues);
     }
public int deleteAll(){
      return sqLiteDatabase.delete(MYDATABASE_TABLE2, null, null);
     }
 public  Cursor queueAll(){
      String[] columns = new String[]{KEY_ID, KEY_VCODE, KEY_VNAME,KEY_DIVSEC};
      Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE2, columns,
        null, null, null, null, null);
      return cursor;
     }
 public String getDivSec(String vname)
 {
     Cursor c=sqLiteDatabase.rawQuery("SELECT * FROM " + MYDATABASE_TABLE2 + " WHERE "+KEY_VNAME +"='"+ vname+"'",null);
     c.moveToFirst();
     String divsec="";
     if (c!=null)
     {
        divsec=c.getString(3);
     }
     c.close();
     return divsec;
 }

 public String[] getAllVillage()
 {
     int i=0;
     Cursor c=sqLiteDatabase.rawQuery("SELECT * FROM " + MYDATABASE_TABLE2 ,null);
     String[] villagelist=new String[c.getCount()];
     if (c.moveToFirst())
     {
         do
         {
             villagelist[i]=c.getString(2);
             i++;
         }while(c.moveToNext());
     }
     c.close();
    return villagelist;
 }
 public String getVcode(String vname)
 {
     Cursor c=sqLiteDatabase.rawQuery("SELECT * FROM " + MYDATABASE_TABLE2 + " WHERE "+KEY_VNAME +"='"+ vname+"'",null);
     c.moveToFirst();
     String vcode="";
     if (c!=null)
     {
        vcode=c.getString(1);
     }
     c.close();
     return vcode;
 }

 public class SQLiteHelper extends SQLiteOpenHelper
 {

    public SQLiteHelper(Context context, String name,
            CursorFactory factory, int version) {
        super(context, name, factory, version);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(SCRIPT_CREATE_DATABASE1);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

 }

}
3
  • 1
    Post some code, or more of a description other than "it won't show any data". Commented Feb 1, 2013 at 5:29
  • I use so many activities, and I did manythings with database. Which one I should post? My question is it works well on emulator. Why not on device? Commented Feb 1, 2013 at 5:31
  • Also, I checked the device sdcard. It won't show the package name under \data Commented Feb 1, 2013 at 5:32

2 Answers 2

1

Normally Sqlite data are stored in

 //data/data/<Your-Application-Package-Name>/databases/<your-database-name>

But you can't see them unless you have root access. So You can store your database directly in SD card like this:

 static class  SQLiteHelper extends SQLiteOpenHelper {

 SQLiteHelper(final Context context) {
    super(context, Environment.getExternalStorageDirectory()
            + File.separator + "/DataBase/" + File.separator
            + DATABASE_NAME, null, DATABASE_VERSION);
}

and now you can see created database in DataBase folder in /sdcard If you store ur databse in internal memory then you can also copy ur database from internal memory to SD card :)

Sign up to request clarification or add additional context in comments.

5 Comments

No no no .in your case this class is <SQLiteHelper> ...i created this class different to handle all my databse..wait i am editing my ans ..now see
I think SQLiteHelper is reserved keyaword
yes its a good practice to create it separate to handle and create you database...but ya its a reserved keyword so you can name something else but i think its fine to do
If i create a class and wrote the above code, will it automatically store database without anything? I mean without any call to this class
go through this link you will get an idea developer.android.com/guide/topics/data/data-storage.html#db if your main class if different just call its constructor from there .. SQLiteHelper s= new SQLiteHelper(this);
0

Yes you need to copy that database to the android device.Refer this tutorial for the solution.

2 Comments

refer full tutorial,i am using it succesfully for my android app
Why do i copy to asset folder?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.