SQLiteOpenHelper - 在 SD 卡上创建数据库 [英] SQLiteOpenHelper - creating database on SD card

查看:26
本文介绍了SQLiteOpenHelper - 在 SD 卡上创建数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的测试 android 应用程序中,我打算创建和访问数据库文件,该文件将位于 SD 卡上.我在类的帮助下使用主要活动,该类扩展了 SQLiteOpenHelper.我想像以前一样使用它,但我必须以某种方式更改数据库路径.您知道如何实现吗?

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?

谢谢

我当前扩展 SQLiteOpenHelper 的类的代码:

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);

}

和我的主要代码:

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();
    }
  }



}

推荐答案

首先要指定sdcard的路径.您可以通过创建这样的字符串来做到这一点:

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";

但是你应该打电话给

Environment.getExternalStorageDirectory()   

获取 SD 卡的根路径并使用它来创建数据库.之后,您可以根据需要创建数据库.这是一个例子

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;
}

最后你必须像这样在清单中设置权限:android.permission.WRITE_EXTERNAL_STORAGE

And in the end you have to set permission in manifest like this: android.permission.WRITE_EXTERNAL_STORAGE

祝你好运:)阿克德

这篇关于SQLiteOpenHelper - 在 SD 卡上创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆