2012-03-09 71 views
0

嘿傢伙我的問題是我的應用程序需要一個外部數據庫已經創建,我看了,發現這個索爾http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/ 這個副本數據庫,但我想訪問表和數據在表中設置一些文本字段訪問數據庫從資產和geting數據從

 private static String DB_PATH = "/data/data/mill.to.want.who/databases/"; 
    private static String DB_NAME = "QuestionDatabase.dp"; 
     public DataBaseHelper(Context context) { 
     super(context, DB_NAME, null, 1); 
     this.myContext = context; 
    } 
    public void createDataBase() throws IOException { 
     boolean dbExist = checkDataBase(); 
     SQLiteDatabase db_Read = null; 
     if (dbExist) {   
     } else { 
      db_Read=this.getReadableDatabase(); 
      db_Read.close(); 
      try { 
       copyDataBase(); 

      } catch (IOException e) { 
      } } } 
    private void copyDataBase() throws IOException { 
     // Open your local db as the input stream 
     InputStream myInput = myContext.getAssets().open(DB_NAME); 
     // Path to the just created empty db 
     String outFileName = DB_PATH + DB_NAME; 
     // Open the empty db as the output stream 
     OutputStream myOutput = new FileOutputStream(outFileName);   

     byte[] buffer = new byte[1024]; 
     int length; 
     while ((length = myInput.read(buffer)) > 0) { 
      myOutput.write(buffer, 0, length); 
     } 
     // Close the streams 
     myOutput.flush(); 
     myOutput.close(); 
     myInput.close(); 
    } 
      public void openDataBase() throws SQLException { 
     // Open the database 
     String myPath = DB_PATH + DB_NAME; 
     myDataBase = SQLiteDatabase.openDatabase(myPath, null, 
       SQLiteDatabase.OPEN_READONLY);} 

感謝我一直掙扎了幾個小時

回答

0

與SQLite數據庫在android系統打交道時,我也跟着這篇文章。

http://www.vogella.de/articles/AndroidSQLite/article.html

+0

謝謝你,但鏈接中的所有數據庫都是硬編碼的。我想使用一個獨立的數據庫文件並從 – ray 2012-03-09 01:53:42

+0

獲取數據請仔細閱讀所有文章,並關注'8.4。創建ContentProvider'。我希望這部分能幫助你。 – PhatHV 2012-03-09 02:06:11