SQLiteOpenHelper - creating database on SD card?

First you have to specify the path of the sdcard. You can do that by creating a string like this.

Up vote 1 down vote favorite share g+ share fb share tw.

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(); } } } android database sqlite sd-card sqliteopenhelper link|improve this question asked Aug 29 '11 at 11:26Waypoint953525 86% accept rate.

Duplicate of SQLiteOpenHelper problem with fully qualified DB path name – k3b Feb 7 at 0:01.

First you have to specify the path of the sdcard. You can do that by creating a string like this: public static final String DATABASE_FILE_PATH = "/sdcard"; But for you should call Environment. GetExternalStorageDirectory() 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 public class DatabaseHelper { private static final String TAG = "DatabaseHelper"; public static final String DATABASE_FILE_PATH = Environment. GetExternalStorageDirectory(); public static final String DATABASE_NAME = "mydb"; public static final String TRACKS_TABLE = "tracks"; public static final String TRACK_INFO_TABLE = "track_info"; private static final String TRACKS_TABLE_CREATE = "create table " + TRACKS_TABLE + " (_id integer primary key autoincrement, title text not null, description text null, created_at date not null);"; private static final String TRACK_INFO_TABLE_CREATE = "create table " + TRACK_INFO_TABLE + " (_id integer primary key autoincrement, track_id integer not null, latitude real not null, longitude real not null, altitude real not null, created_at date not null);"; private SQLiteDatabase database; public DatabaseHelper() { try { database = SQLiteDatabase.

OpenDatabase(DATABASE_FILE_PATH + File. Separator + DATABASE_NAME, null,SQLiteDatabase. OPEN_READWRITE); } catch (SQLiteException ex) { Log.

E(TAG, "error -- " + ex.getMessage(), ex); // error means tables does not exits createTables(); } finally { DBUtil. SafeCloseDataBase(database); } } private void createTables() { database. ExecSQL(TRACKS_TABLE_CREATE); database.

ExecSQL(TRACK_INFO_TABLE_CREATE); } public void close() { DBUtil. SafeCloseDataBase(database); } public SQLiteDatabase getReadableDatabase() { database = SQLiteDatabase. OpenDatabase(DATABASE_FILE_PATH + File.

Separator + DATABASE_NAME, null, SQLiteDatabase. OPEN_READONLY); return database; } public SQLiteDatabase getWritableDatabase() { database = SQLiteDatabase. OpenDatabase(DATABASE_FILE_PATH + File.

Separator + DATABASE_NAME, null, SQLiteDatabase. OPEN_READWRITE); return database; } And in the end you have to set permission in manifest like this: android.permission. WRITE_EXTERNAL_STORAGE Good luck :) Arkde.

– Waypoint Aug 29 '11 at 11:54 DatabaseHelper is a class and have a constructor that handle database opening and create operations. Create a new databasehelper object in your main activity and use it's metods. – Arkde Aug 29 '11 at 12:20 I don't know, where you have copied it, but it doesn't solve it.

I had the same code before and it wasn't working, thus I have asked for a solution with my own code. – Waypoint Aug 29 '11 at 12:52 The code is just an example of how you can do it. Have you set up write external storage permission in manifest?

– Arkde Aug 29 '11 at 13:22 Yep, everything like is stated here – Waypoint Aug 29 '11 at 14:17.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions