类SQLiteOpenHelper附带的文档没有说明其方法getReadableDatabase查找哪个目录来获取数据库。
我试图理解RIP教程中以下代码片段中第31行this.getReadableDatabase
的语句的必要性:
01 public DatabaseHelper(Context context) {
02 super(context, DB_NAME, null, 1);
03 this.myContext = context;
04 ContextWrapper cw = new ContextWrapper(context);
05 // The line below will define the path to the database file in the apps file directory
06 // as being the internal file directory/databases/
07 DB_PATH = cw.getFilesDir().getAbsolutePath() + "/databases/";
08 Log.e(TAG, "Databasehelper: : DB_PATH is: " + DB_PATH);
09 outFileName = DB_PATH + DB_NAME;
10 File file = new File(DB_PATH);
11 Log.e(TAG, "DatabaseHelper does file exist? : " + file.exists());
12 // File file is just the directory part of entire path so file.mkdir() ensures the
13 // directory is created.
14 if (!file.exists()) {
15 file.mkdir();
16 }
17
18 }
19
20 /*
21 * Creates empty data database on the system and rewrites it with your own database.
22 */
23
24 public void createDatabase() throws IOException {
25 boolean dbExist = checkDatabase();
26 if (dbExist) {
27 // do nothing - database already exists
28 } else {
29 // By calling this method an empty database will be created into the files directory
30 // of your app. We will be able to overwrite this database with our database
31 this.getReadableDatabase();
32 try {
33 copyDatabase();
34 } catch (IOException e) {
35 throw new Error("Error copying database");
36 }
37 }
38 }
,,,
行super(context, DB_NAME, null, 1);
设置所使用的数据库名称,该名称始终位于应用程序的私有";数据库";目录