1 public class MyDB extends SQLiteOpenHelper {
2 // 数据库的缺省路径
3 private static String DB_PATH ;
4 private static final String DB_NAME = "aa.db";
5 private static final int DB_VERSION = 2;
6 private SQLiteDatabase m_database;
7 private final Context m_context;
8 /**
9 *
10 * Constructor
11 *
12 * 保存传进来的context参数以用来访问应用的asset和raw文件。
13 *
14 * @param context
15 */
16 public MyDB(Context context) {
17 super(context, DB_NAME, null, DB_VERSION);
18 this.m_context = context;
19 DB_PATH ="/data/data/"+context.getPackageName()+"/databases/";
20 }
21 public static MyDB openDatabaseReadOnly(Context context) {
22 MyDB db = new MyDB(context);
23 try {
24 db.createDataBase();
25 } catch (IOException e) {
26 e.printStackTrace();
27 }
28 db.openDataBase(SQLiteDatabase.OPEN_READONLY);
29 return db;
30 }
31 public static MyDB openDatabaseReadWrite(Context context) {
32 MyDB db = new MyDB(context);
33 try {
34 db.createDataBase();
35 } catch (IOException e) {
36 e.printStackTrace();
37 }
38 db.openDataBase(SQLiteDatabase.OPEN_READWRITE);
39 return db;
40 }
41 /**
42 *
43 * 创建一个空数据库,用来存储你已有的数据库。
44 */
45 public void createDataBase() throws IOException {
46 boolean dbExist = checkDataBase();
47 if (dbExist) {
48 // 如果你的数据库的版本改变了,调用这个方法确保在onUpgrade()被调用时
49 // 传进去的是可写的数据库。
50 SQLiteDatabase db = this.getWritableDatabase();
51 if (db != null) {
52 db.close();
53 }
54 }
55 dbExist = checkDataBase();
56 if (!dbExist) {
57 try {
58 // 调用这个方法以确保在缺省路径内产生一个空数据库,以便在其基础上复制我们已有的数据库。
59 SQLiteDatabase db = this.getReadableDatabase();
60 if (db != null) {
61 db.close();
62 }
63 copyDataBase();
64 } catch (IOException e) {
65 Log.e("DB", e.getMessage());
66 throw new Error("Error copyingdatabase");
67 }
68 }
69 }
70 /**
71 *
72 * 检查数据库是否已存在,以避免重复复制。
73 *
74 * @return true if it exists, false if itdoesn't
75 */
76 private static boolean checkDataBase() {
77 SQLiteDatabase checkDB = null;
78 try {
79 String path = DB_PATH + DB_NAME;
80 checkDB = SQLiteDatabase.openDatabase(path, null,SQLiteDatabase.OPEN_READONLY);
81 }
82 catch (SQLiteException e) {
83 e.printStackTrace();
84 }
85 if (checkDB != null) {
86 checkDB.close();
87 }
88 return checkDB != null ? true : false;
89 }
90 /**
91 *
92 * 把存在asset文件中的数据库复制的刚创建的空数据库中。
93 *
94 * */
95 private void copyDataBase() throws IOException {
96 // 刚创建的空数据库的路径
97 String outFileName = DB_PATH + DB_NAME;
98 // 打开空数据库
99 OutputStream output = new FileOutputStream(outFileName);
100 byte[] buffer = new byte[1024];
101 // AssetManager assetMgr = m_context.getAssets();
102 // 打开分解的asset文件
103 // InputStream input = assetMgr.open(fn);
104 InputStream input =m_context.getResources().openRawResource(R.raw.area);
105 int length;
106 while ((length = input.read(buffer)) !=-1) {
107 output.write(buffer, 0, length);
108 }
109 input.close();
110 output.flush();
111 output.close();
112 }
113 /**
114 *
115 * 打开数据库。
116 *
117 * */
118 private void openDataBase(int flags) throws SQLException {
119
120 String myPath = DB_PATH + DB_NAME;
121 m_database = SQLiteDatabase.openDatabase(myPath, null, flags);
122 }
123 /**
124 *
125 * 关闭数据库。
126 *
127 * */
128 @Override
129 public synchronized void close() {
130 if (m_database != null)
131 m_database.close();
132 super.close();
133 }
134 @Override
135 public void onCreate(SQLiteDatabase db) {}
136 /**
137 *
138 * 在数据库版本提高时,删除原有数据库。
139 *
140 * */
141 @Override
142 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
143 if (newVersion > oldVersion) {
144 m_context.deleteDatabase(DB_NAME);
145 }
146 }
147 }


String path = DB_PATH + DB_NAME;

   checkDB = SQLiteDatabase.openDatabase(path, null,SQLiteDatabase.OPEN_READONLY);