1

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);
  • I would like to create the database on the external sd card, you can do it? – user3611182 May 13 '14 at 17:07
  • Actually, the db is created in `/data/data/your.app.name/databases/your.db`, But you can specify the path to your sd card, as explained in this answer: http://stackoverflow.com/a/7229616/2649012 – Phantômaxx May 13 '14 at 17:11
  • I thank you for reply, I saw that post, but maybe for my poor English, I could not figure out how to do it. – user3611182 May 13 '14 at 17:20

1 Answers1

1

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.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115