6

I have just created database,its stored in data/data/database folder by default.Now i wanna store database in sdcard,I have used following code.please suggest me where have to add code for that.Thanks

Code:

    public class DataBaseHandler {
    SQLiteDatabase database;
    private static DataBaseHandler obj;
    private final String TABLE_NAME = "main_db";
    private final String COLUMN_FIRST = "_id";
    MovieDetails md = new MovieDetails();

    private DataBaseHandler(Context context) {
        DataBase dbobj = new DataBase(context, "Id_db.db", null, 1);
        database = dbobj.getWritableDatabase();
    }

    public static DataBaseHandler getinstance(Context context) {
        if (obj == null) {
            obj = new DataBaseHandler(context);
        }
        return obj;
    }

    public void insertData(String id) {
        try {
            ContentValues values = new ContentValues();
            values.put(COLUMN_FIRST, id);
            database.insert(TABLE_NAME, null, values);
        } catch (Exception er) {
            Log.d("Error is===", er.toString());
        }
    }

    public void deleteData(String id) {
        database.delete(TABLE_NAME, BaseColumns._ID + "=" + id, null);
    }

    public Cursor getData() {
        String[] columns = { BaseColumns._ID, COLUMN_FIRST };
        return database
                .query(TABLE_NAME, columns, null, null, null, null, null);
    }

    private class DataBase extends SQLiteOpenHelper {
        public DataBase(Context context, String name, CursorFactory factory,
                int version) {
            super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE " + TABLE_NAME + "(" + BaseColumns._ID
                    + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_FIRST
                    + " varchar(50)NOT NULL UNIQUE);");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        }
    }
}
Dilip
  • 2,271
  • 5
  • 32
  • 52
  • possible duplicate of [Is it possible to move the internal DB to the SDCard?](http://stackoverflow.com/questions/3436434/is-it-possible-to-move-the-internal-db-to-the-sdcard) – jfs Aug 27 '12 at 06:49
  • Thanks @jfs for reply,But can you tell me in my above code where have to put code for store database in Sdcard.... – Dilip Aug 27 '12 at 06:52

1 Answers1

2

Use this class to copy your DB:

public class MyDatabaseTools {
  private String appName = "";
  private String packageName = "";

  public boolean backup() {
    boolean rc = false;

    boolean writeable = isSDCardWriteable();
    if (writeable) {
      File file = new File(Environment.getDataDirectory() + "/data/" + packageName + "/databases/" + MySQLiteOpenHelper.getDatabaseName());

      File fileBackupDir = new File(Environment.getExternalStorageDirectory(), appName + "/backup");
      if (!fileBackupDir.exists()) {
        fileBackupDir.mkdirs();
      }

      if (file.exists()) {
        File fileBackup = new File(fileBackupDir, MySQLiteOpenHelper.getDatabaseName());
        try {
          fileBackup.createNewFile();
          FileUtils.copyFile(file, fileBackup);
          rc = true;
        } catch (IOException ioException) {
          //
        } catch (Exception exception) {
          //
        }
      }
    }

    return rc;
  }

  private boolean isSDCardWriteable() {
    boolean rc = false;

    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
      rc = true;
    }

    return rc;
  }

    public MyDatabaseTools(final Context context, final String appName) {
        this.appName = appName;
        packageName = context.getPackageName();
    }
}

Please refer to the Source if you dont understand how to use it.

Community
  • 1
  • 1
Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
  • Implementing it in the above code would not be a good thing. You can call this class in an activity when you finish creating your database. – Lazy Ninja Aug 27 '12 at 07:37
  • Tell me one think @Ninja,when we create database i will be saved in data/data/databases folder but i don't wanna save this here i have to save it in sdcard.Is this possible or not if possible how.. – Dilip Aug 27 '12 at 08:18
  • Yes. Use openDatabase() You can put and open the database from wherever you like, i.e. SQLiteDatabase.openDatabase("/sdcard/mydatabase.db", null, SQLiteDatabase.OPEN_READONLY); – Lazy Ninja Aug 27 '12 at 08:25
  • getting android.database.sqlite.SQLiteCantOpenDatabaseException: so please help me in this context..My code is private DataBaseHandler(Context context) { DataBase dbobj = new DataBase(context, DB_NAME, null, 1); database = dbobj.getWritableDatabase(); SQLiteDatabase.openDatabase("/sdcard/Id_db.db", null, SQLiteDatabase.OPEN_READONLY); } – Dilip Aug 27 '12 at 08:49
  • This link will help you http://stackoverflow.com/a/7229616/1503155. If you have problems let me know. – Lazy Ninja Aug 27 '12 at 09:08
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/15852/discussion-between-dilip-and-lazy-ninja) – Dilip Aug 27 '12 at 12:57
  • Sorry I didnt see your you last message. Do you still have problems? – Lazy Ninja Aug 28 '12 at 00:55
  • yeah,Actually i'm very confused about this.I have seen lot of example and every example saying copy database in sdcard(Its not good way in my context)I don't have to use data/data/packageNmae...only have to use sdcard for store database and perform operation from there.How can be done this please tell me in deep, if possible provide me some example....Thanks.. – Dilip Aug 28 '12 at 07:13