Android - SQLite Database on SD Card

Is there any way how to create and use the database from the SD card in my application instead of the /data/data/com.myapp/databases directory? I know this is unsafe, but are there any special restrictions, such as "the database on the SD card cannot be larger than 2 GB"?

thanks

Hmyzak

+4
source share
2 answers

he is the proposed solution i found on 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's a link .

UPDATE

I'm not sure you can use this with SQLiteOpenHelper, but you can query a 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 whatever you expect with a database. SQLiteOpenHelper is only in a wrapper class, which helps you additionally, which we can always do on our own.

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

+6
source

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) { } } 

Using:

  ExternalDBHelper helper = new ExternalDBHelper(context,dbName, CLeitner.Constant.DATABASE_VERSION); SQLiteDatabase db = helper.getWritableDatabase(); 
0
source

Source: https://habr.com/ru/post/1369451/


All Articles