سؤال

In my application I use a sqlite database, which is currently being created in the phone memory and can create the database on the sdcard

public class DbHelper extends SQLiteOpenHelper {
    static String DATABASE_NAME="contiDB.db";
    public static final String CONTI="turni";
    public static final String CONTI_ID="idturno";
    public static final String ANNO="anno";
    public static final String MESE="mese";
    public static final String DATA="data";


    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_TABLE="CREATE TABLE "+CONTI+" ("+CONTI_ID+" INTEGER PRIMARY KEY,"+DATA+" NUMERIC,"+MESE+" TEXT,"+ANNO+" NUMERIC)";
        db.execSQL(CREATE_TABLE);
هل كانت مفيدة؟

المحلول

Actually, the db is created in /data/data/your.app.name/databases/your.db,
but you can specify the path to your sd card

In short:

  • Set this permission in your Manifest file:

    <!-- To write and read to and from SDCard -->  
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
  • get the path to your sd card

    public static final String  DATABASE_FILE_PATH = Environment.getExternalStorageDirectory();
    
  • When opening your db (getWitableDatabase, getReadableDatabase), specify you path:

    database = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH
        + File.separator + DATABASE_NAME, null,SQLiteDatabase.OPEN_READWRITE);
    

    or

    database = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH
        + File.separator + DATABASE_NAME, null,SQLiteDatabase.OPEN_READONLY);
    

    respectively

[EDIT]

Note that WRITE_EXTERNAL_STORAGE includes READ_EXTERNAL_STORAGE, so, no need to specify this one too.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top