SQLiteOpenHelper - creating a database on an SD card

in my test Android application, I intend to create and access the database file, which will be located on the SD card. I use the main action with a class that extends SQLiteOpenHelper. I want to use it the same way as before, but I need to somehow modify the PATH database. Do you know how to achieve it?

THX

My current class code that 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 my main code:

 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(); } } } 
+5
source share
2 answers

First you must specify the path to the SD card. You can do this by creating a line like this:

 public static final String DATABASE_FILE_PATH = "/sdcard"; 

But you have to call

 Environment.getExternalStorageDirectory() 

to get the root path to the SD card and use it to create the database. After that, you create the database as you wish. 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 at the end, you should set the resolution in the manifest as follows: android.permission.WRITE_EXTERNAL_STORAGE

Good luck :) Arkde

+3
source

If some of you want to create a sqlite database on an SD card, but you still want to continue to use a class that extends sqliteOpenHelper , you can visit this answer .

You just need to add another class, implement it in your class (which extends sqliteOpenHelper ) to super (), and then change the path in the new class.

Note. If you are using HC or ICS, you need to describe "external_sd" in your path. Because HC and ICS will still report storage that is physically embedded in the device

0
source

Source: https://habr.com/ru/post/923825/


All Articles