android – SQLiteOpenHelper – 在SD卡上创建数据库

前端之家收集整理的这篇文章主要介绍了android – SQLiteOpenHelper – 在SD卡上创建数据库前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在我的测试Android应用程序中,我打算创建和访问数据库文件,它将位于SD卡上.我在一个类的帮助下使用main活动,该类扩展了sqliteOpenHelper.我想以与以前相同的方式使用它,但我必须以某种方式更改数据库PATH.你知道吗,如何实现它?

谢谢

我当前扩展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();
    }
  }



}
最佳答案
首先,您必须指定SD卡的路径.你可以通过创建一个这样的字符串来做到这一点:

public static final String  DATABASE_FILE_PATH = "/sdcard";

但是你应该打电话

Environment.getExternalStorageDirectory()   

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

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,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,sqliteDatabase.OPEN_READONLY);
    return database;
}

public sqliteDatabase getWritableDatabase()
{
    database = sqliteDatabase.openDatabase(DATABASE_FILE_PATH
            + File.separator + DATABASE_NAME,sqliteDatabase.OPEN_READWRITE);
    return database;
}

最后,您必须在清单中设置权限,如下所示:android.permission.WRITE_EXTERNAL_STORAGE

祝好运 :)
Arkde

原文链接:https://www.f2er.com/android/430473.html

猜你在找的Android相关文章