6

is there any way, how to create and use database from SD card in my app instead of /data/data/com.myapp/databases directory? I know is it unsecure, but are there any special restriction like "database on SD card cannot be bigger then 2GB"?

Thanks

Hmyzak

Waypoint
  • 17,283
  • 39
  • 116
  • 170

2 Answers2

9

he is a proposed solution which i found in stackoverflow

File dbfile = new File("/sdcard/android/com.myapp/databases/mydatabase.db" ); 
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
System.out.println("Its open? "  + db.isOpen());

here is the link.

UPDATE

i am not sure you can use this along with SQLiteOpenHelper, but you sure can query the database object.

db.getVersion();
db.execSQL(sql);
db.beginTransaction();
db.endTransaction();
db.setTransactionSuccessful();
db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);

you can do all the things which you expect with a database. SQLiteOpenHelper in only a wrapper class which helps you the extra which we always can do on our own.

EDIT as for your file size limit i found this link. Is there a file size limit on Android Honeycomb?

Community
  • 1
  • 1
Samuel
  • 9,883
  • 5
  • 45
  • 57
  • 3
    Nice but, how to use it with SQLiteOpenHelper class, which defines my database? – Waypoint Aug 29 '11 at 11:18
  • @WayPoint, In my case, I have no need another chunk or helper, With the above way all work is done easily, Thanks@samuel, struggling all the day I found it at last, Alhamdulillah. – Noor Hossain Oct 08 '20 at 19:31
0

You can use this class :

public class ExternalDBHelper  extends SQLiteOpenHelper {


    public ExternalDBHelper(Context context, String DATABASE_NAME,  int DATABASE_VERSION) {
        super(context, DirectoryManager.getDir(context, DirectoryManager.ChildDir.Databases, DirectoryManager.ForcePath.SDCard)
                + DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}

Use :

 ExternalDBHelper helper = new ExternalDBHelper(context,dbName,  CLeitner.Constant.DATABASE_VERSION);
 SQLiteDatabase db = helper.getWritableDatabase();
Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98