从外部SD卡获取路径时定义opendatabase字符串参数

时间:2013-09-18 07:26:13

标签: android sqlite

//this is my handler class


  public class DBHandler extends SQLiteOpenHelper{

  private static final int DATABASE_VERSION = 1;    
  private static final String DATABASE_NAME = "feedbackDB.db"; 
  private static final String TABLE_FEEDBACK = "feedback";

  public DBHandler(Context context) {

       super(context,context.getExternalFilesDir(null).getAbsolutePath()+DATABASE_NAME, null, DATABASE_VERSION);

        }



  public void openDataBase(SQLiteDatabase db) throws SQLException {     

          //Open the database       
          String myPath = context.getExternalFilesDir(null).getAbsolutePath() + DATABASE_NAME;  

          db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);     
          }

1 个答案:

答案 0 :(得分:1)

在构造函数中声明DB_PATH值并随时使用它,因此您不需要每次都分配

public DBHandler(Context context)
{

    super(context, DB_NAME, null, 1);
    this.myContext = context;
    DB_PATH = Environment.getExternalStorageDirectory()
            + File.separator + "/.folder name/" + File.separator;

    File f = new File(DB_PATH);
    f.mkdirs();


}

并将其用作DB_PATH+DATABASE_NAME来执行数据库操作