2012-03-22 3 views
1

나는 안드로이드 응용 프로그램에서 내 자신의 데이터베이스를 사용하려고합니다. 이 항목에 대한 대부분의 모든 항목과 자습서에서는 SQLiteOpenHelper을 사용하여 폴더를 만들고 열 수 있음을 제안합니다.이 폴더는 기본적으로 data/data/com.my.application/databases 폴더를 사용합니다. context.getAssets.open(dbname)에서 기본 sqlite 경로로 파일을 복사하는 것이 좋습니다. 이것이 실제로 해결책 일 경우, 내 database.db 파일을 전달하는 올바른 경로는 무엇입니까?내 디렉토리에 안드로이드 데이터베이스가

+0

그래서 당신이 어떤 경로를 알고 싶어이 하나 개의 코드를 시도 할 수 있습니다? 목적지? –

+0

예. 그렇습니다. 프로젝트의 "assets"폴더에 데이터베이스 파일을 넣어야한다고 생각했습니다. 여러분이'onCreate' /'onUpgrade' 메카니즘을 사용하지 않으면'extends SQLiteOpenHelper' 부분을 삭제하십시오. – cgalvao1993

답변

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

. 올바르게 사용하면 제대로 작동하지만, 많은 에러를 줄 수 있습니다. 왜냐하면'.getWritableDatabase'를 통해 db를 얻으면 빈 데이터베이스를 줄 수 있기 때문입니다 (onCreate에 코드가 없으므로 – zapl

관련 문제