Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store Android SQLite database in sdcard

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) {
        }
    }
}
like image 425
Dilip Avatar asked Aug 27 '12 06:08

Dilip


1 Answers

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.

like image 158
Lazy Ninja Avatar answered Sep 22 '22 17:09

Lazy Ninja