开发者

Android - SQLite database on SD card

开发者 https://www.devze.com 2023-04-01 13:23 出处:网络
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 restrict开发者_运维百科ion

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 restrict开发者_运维百科ion like "database on SD card cannot be bigger then 2GB"?

Thanks

Hmyzak


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?


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();
0

精彩评论

暂无评论...
验证码 换一张
取 消