SQLiteOpenHelper - creating database on SD card

天涯浪子 提交于 2019-11-27 09:20:02
Aurelian Cotuna

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

Rjrain

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!