1. public class DatabaseHelper extends SQLiteOpenHelper { 
  2.     private final String packageName = "应用的包名"
  3.     private String DB_PATH = "/data/data/" + packageName + "/databases/"; 
  4.     private SQLiteDatabase myDataBase; 
  5.     private final Context myContext; 
  6.     private static final String DB_NAME = "soft.db"
  7.     private static String CITY = "city_name"
  8.     public static final String CITY_ID = "city_id"
  9.     public static final String CITY_NAME = "city_name"
  10.  
  11.     public DatabaseHelper(Context context) { 
  12.         super(context, DB_NAME, null, 1); 
  13.         this.myContext = context
  14.     } 
  15.  
  16.     /** 
  17.      * 创建空数据库,并且通过复制资源文件夹的数据库覆盖 
  18.      * */ 
  19.     public void createDataBase() throws IOException { 
  20.         boolean dbExist = checkDataBase(); 
  21.         if (dbExist) {// 存在,什么也不干 
  22.  
  23.         } else { 
  24.             // 进行数据库的复制 
  25.             this.getReadableDatabase(); 
  26.             try { 
  27.                 copyDataBase(); 
  28.             } catch (IOException e) { 
  29.                 throw new Error("Error copying database"); 
  30.             } 
  31.         } 
  32.     } 
  33.  
  34.     /** 
  35.      * 查询操作 
  36.      *  
  37.      * @return 
  38.      */ 
  39.     public Cursor getAllCity() { 
  40.         String col[] = new String[] { CITY_ID, CITY_NAME }; 
  41.         // return db.rawQuery("select * from tb_diary",null); 
  42.         return myDataBase.query(CITY, col, null, null, null, null, null); 
  43.     } 
  44.  
  45.     /** 
  46.      * 启动应用的时候去检查数据库是否存在,如果不存在,则需要调用copy数据库的方法 
  47.      *  
  48.      * @return 存在返回 true , 不存在返回false 
  49.      */ 
  50.     boolean checkDataBase() { 
  51.         SQLiteDatabase checkDB = null
  52.         try { 
  53.             String myPath = DB_PATH + DB_NAME; 
  54.             checkDB = SQLiteDatabase.openDatabase(myPath, null, 
  55.                     SQLiteDatabase.OPEN_READONLY); 
  56.         } catch (SQLiteException e) { 
  57.             // 如果数据库不存在 
  58.             // throw new Error("Database does't exist yet."); 
  59.             return false; 
  60.         } 
  61.         if (checkDB != null) { 
  62.             checkDB.close(); 
  63.         } 
  64.         return checkDB != null ? true : false; 
  65.     } 
  66.  
  67.     /** 
  68.      *  
  69.      * 复制数据库的方法 
  70.      *  
  71.      * @throws IOException 
  72.      *  
  73.      * */ 
  74.     void copyDataBase() throws IOException { 
  75.         // 通过资源文件访问数据库文件,以输入流的形式 
  76.         InputStream myInput = myContext.getAssets().open(DB_NAME);// 访问assets下的资源 
  77.         // InputStream 
  78.         // input=myContext.getResources().openRawResource(R.id.xx);//访问res下的资源 
  79.         // 数据库的 输出地址 
  80.         String outFileName = DB_PATH + DB_NAME; 
  81.         OutputStream myOutput = new FileOutputStream(outFileName); 
  82.         byte[] buffer = new byte[1024]; 
  83.         int length; 
  84.         while ((length = myInput.read(buffer)) > 0) { 
  85.             myOutput.write(buffer, 0, length); 
  86.             Log.i("data", "copying....."); 
  87.         } 
  88.         myOutput.flush(); 
  89.         myOutput.close(); 
  90.         myInput.close(); 
  91.  
  92.     } 
  93.  
  94.     /** 
  95.      * 关闭数据库的方法 
  96.      */ 
  97.     @Override 
  98.     public synchronized void close() { 
  99.         if (myDataBase != null) 
  100.             myDataBase.close(); 
  101.         super.close(); 
  102.     } 
  103.  
  104.     @Override 
  105.     public void onCreate(SQLiteDatabase db) { 
  106.     } 
  107.  
  108.     @Override 
  109.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
  110.     } 
  111.  
  112.     /** 
  113.      * 打开数据库的方法 
  114.      *  
  115.      * @throws SQLException 
  116.      */ 
  117.     public void openDataBase() throws SQLException { 
  118.         // Open the database 
  119.         String myPath = DB_PATH + DB_NAME; 
  120.         myDataBase = SQLiteDatabase.openDatabase(myPath, null, 
  121.                 SQLiteDatabase.OPEN_READONLY); 
  122.     }