2012-03-22 18 views
1

我想在Android應用程序中使用我自己的數據庫。大多數關於此主題的所有主題和教程都表明您可以使用SQLiteOpenHelper創建它並將其打開,默認情況下使用data/data/com.my.application/databases文件夾。我已經看到了建議將文件從context.getAssets.open(dbname)複製到默認的sqlite路徑。如果這實際上是解決方案,那麼通過我的database.db文件的正確途徑是什麼?Android數據庫在我自己的目錄中

+0

那麼你想知道哪些路徑?目的地? – 2012-03-22 17:46:28

+0

是的,就是這種情況..我想我不得不把數據庫文件放在項目 – cgalvao1993 2012-03-22 17:59:56

回答

0

我的朋友,你可以試試這個代碼

/** 
* 
* Purpose: Open and Read database to fetch records from different database 
* tables. 
* 
* @version 1.0 
* 
* @author ketan kalariya([email protected]) 
*   
* **/ 

    public class DataBaseHelper extends SQLiteOpenHelper { 

     private Context myContext; 
     private SQLiteDatabase myDataBase; 
     // Comman KEYs of tables 
     public static String MASTERFENCE_TABLE = "MasterFence"; 
     public static String KEY_ID = "_id"; 
     public static String AREA = "area"; 
     public static String TAG = "tag"; 
     public static String TITLE = "title"; 
     public static String DESCR = "description"; 
     public static String NO = "no"; 

     public static String ADDFENCEPOINT_TABLE = "AddFencePoint"; 
     public static String ADDLATLONG_TABLE = "AddLatLong"; 
     public static String LAT = "lat"; 
     public static String LONG = "long"; 

     public static String TEMPFENCEPOINT_TABLE = "TempFencePoint"; 
     public static String TEMPLATLONG_TABLE = "TempLatLong"; 

     public DataBaseHelper(Context context) { 
      super(context, context.getResources().getString(R.string.dbname), null, 
        1); 
      this.myContext = context; 
     } 

     @Override 
     public void onCreate(SQLiteDatabase arg0) { 

     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     } 

     // ---Create the database--- 
     public void createDataBase() throws IOException { 

      // ---Check whether database is already created or not--- 
      boolean dbExist = checkDataBase(); 

      if (!dbExist) { 
       this.getReadableDatabase(); 
       try { 
        // ---If not created then copy the database--- 
        copyDataBase(); 
        this.close(); 
       } catch (IOException e) { 
        throw new Error("Error copying database"); 
       } 
      } 
     } 

     // --- Check whether database already created or not--- 
     private boolean checkDataBase() { 
      try { 
       final String myPath = "/data/data/" + myContext.getPackageName() 
         + "/databases/" + myContext.getString(R.string.dbname); 
       final File f = new File(myPath); 
       if (f.exists()) 
        return true; 
       else 
        return false; 
      } catch (SQLiteException e) { 
       e.printStackTrace(); 
       return false; 
      } 
     } 

     // --- Copy the database to the output stream--- 
     public void copyDataBase() throws IOException { 

      final InputStream myInput = myContext.getAssets().open(
        myContext.getString(R.string.dbname)); 

      final String outFileName = "/data/data/" + myContext.getPackageName() 
        + "/databases/" + myContext.getString(R.string.dbname); 

      final OutputStream myOutput = new FileOutputStream(outFileName); 

      final byte[] buffer = new byte[1024]; 
      int length; 
      while ((length = myInput.read(buffer)) > 0) { 
       myOutput.write(buffer, 0, length); 
      } 

      myOutput.flush(); 
      myOutput.close(); 
      myInput.close(); 
     } 

     public void openDataBase() throws SQLException { 

      // --- Open the database--- 
      final String myPath = "/data/data/" + myContext.getPackageName() 
        + "/databases/" + myContext.getString(R.string.dbname); 

      myDataBase = SQLiteDatabase.openDatabase(myPath, null, 
        SQLiteDatabase.OPEN_READWRITE); 
     } 

     /** 
     * Close the database 
     */ 
     public synchronized void closeDatabase() { 
      if (myDataBase != null) 
       ; 
      myDataBase.close(); 
      SQLiteDatabase.releaseMemory(); 
      super.close(); 
     } 


     /** This method is insert require detail in database table. **/ 
     public long insertMasterFence(String area, String title, String descr, 
       String tag) { 
      ContentValues values = new ContentValues(); 

      values.put(AREA, area); 
      values.put(TITLE, title); 
      values.put(DESCR, descr); 
      values.put(TAG, tag); 
      Log.v(" db ", area + " " + title + " " + " " + descr + " " + tag); 
      return myDataBase.insert(MASTERFENCE_TABLE, null, values); 

     } 
+0

的「assets」文件夾中,如果你不使用'onCreate' /'onUpgrade'機制,請刪除'extends SQLiteOpenHelper'部分。雖然它可能工作,如果你正確使用它會給你很多錯誤,因爲如果你通過'.getWritableDatabase'得到你的數據庫它可能會給你一個空的數據庫(因爲沒有代碼在onCreate – zapl 2012-03-23 10:34:51

相關問題