in my test android app I intend to create and access database file, which will be located on SD card. I am using main activity with help of a class, which extends SQLiteOpenHelper. I want to use it the same way as before, but I have to somehow change the database PATH. Do you know, how to achieve it?
thx
My current code of a class which extends SQLiteOpenHelper:
public class DatabaseDefinition extends SQLiteOpenHelper{
private static final String DATABASE_NAME="test.db";
private static final int DATABASE_VERSION=1;
public DatabaseDefinition(Context context) {
super(context,DATABASE_NAME,null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+TABLE_NAME+" ("+ _ID +" INTEGER PRIMARY KEY AUTOINCREMENT, "+ NAME+" TEXT NOT NULL, " +SURNAME+" TEXT NOT NULL, "+PHONE+" INT NOT NULL);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
onCreate(db);
}
And code of my main:
public class DatabaseExampleActivity extends Activity {
private DatabaseDefinition database;
private static String[] FROM={_ID, NAME, SURNAME,PHONE};
private static String ORDER_BY=" DESC";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
database= new DatabaseDefinition(this);
try{
String name = null;
String surname=null;
int phone=0;
addEvent("John", "Black", 111012345);
}finally{
database.close();
}
}
}
First you have to specify the path of the sdcard. You can do that by creating a string like this:
But for you should call
to get the root path to the SD card and use that to create the database. After that you create the database as you want. Here is an example
And in the end you have to set permission in manifest like this:
android.permission.WRITE_EXTERNAL_STORAGE
Good luck :) Arkde
If some of you want to create sqlite database on sd card but you still want to keep using class which extends
sqliteOpenHelper
, you can visit this answer.You just have to add another class, implement it on your class (which extends
sqliteOpenHelper
) in super(), then change the path on your new class.Note: If you are using HC or ICS you need to describe the "external_sd" on your path. Because HC and ICS will still report storage that's physically built into the device