Android - SQLite database on SD card

前端 未结 2 1829
再見小時候
再見小時候 2020-12-18 06:59

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

相关标签:
2条回答
  • 2020-12-18 07:59

    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?

    0 讨论(0)
  • 2020-12-18 08:00

    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 讨论(0)
提交回复
热议问题