2012-10-09 1 views
3

장치의 데이터 폴더에 액세스하려면 루트 폴더에 있어야합니다. 그러나 자산 폴더에서 데이터베이스의 데이터 폴더로 데이터베이스를 복사하려는 경우 복사되지 않은 전화에서 복사 프로세스가 작동합니까? 다음은 데이터베이스 도우미 클래스입니다. logcat에서 copyDataBase(), createDataBase() 및 openDataBase() 메서드 호출이 성공적으로 반환되는지 확인할 수 있습니다. 그러나, 나는이 오류 메시지가SQLite 파일을 루팅되지 않은 안드로이드 장치의 데이터 폴더에 성공적으로 복사 할 수 있습니까?

android.database.sqlite.SQLiteException을 가지고 : 없음 같은 테이블 : TABLE_NAME :

하는 내 응용 프로그램 rawQuery를 실행한다. 데이터베이스 파일이 성공적으로 복사되지 않았을 것으로 의심됩니다 (데이터 폴더에 대한 액세스 권한이 없으므로 확신 할 수 없음). 그러나 copyDatabase() 메서드 호출은 예외를 던지지 않습니다. 뭐가 될수 있었는지? 감사.

ps : 내 장치가 아직 루트가 지정되지 않았습니다. 오류의 주 원인이 아니기를 바랍니다.

public DatabaseHelper(Context context) { 

     super(context, DB_NAME, null, 1); 
     this.myContext = context; 
    } 

public void createDataBase() throws IOException{ 

     boolean dbExist = checkDataBase(); 

     String s = new Boolean(dbExist).toString(); 
     Log.d("dbExist", s); 

     if(dbExist){ 
      //do nothing - database already exist 
      Log.d("createdatabase","DB exists so do nothing"); 
     }else{ 


     this.getReadableDatabase(); 

      try { 

       copyDataBase(); 
       Log.d("copydatabase","Successful return frm method call!"); 

      } catch (IOException e) { 

       throw new Error("Error copying database"); 

      } 
     } 

    } 


    private boolean checkDataBase(){ 

     File dbFile = new File(DB_PATH + DB_NAME); 
     return dbFile.exists(); 
      } 


    private void copyDataBase() throws IOException{ 

     //Open your local db as the input stream 
     InputStream myInput = null; 


      myInput = myContext.getAssets().open(DB_NAME); 


      Log.d("copydatabase","InputStream successful!"); 


     // 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); 

     //transfer bytes from the inputfile to the outputfile 
     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); 

    } 

/* @Override 
    public synchronized void close() { 

      if(myDataBase != null) 
       myDataBase.close(); 

      super.close(); 

    }*/ 

    public void close() { 
     // NOTE: openHelper must now be a member of CallDataHelper; 
     // you currently have it as a local in your constructor 
     if (myDataBase != null) { 
      myDataBase.close(); 
     } 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 

    } 

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

    } 



} 
+0

는 소스 코드와 함께,이 완전한/작업 자습서를보십시오 : 가 http://stackoverflow.com/questions/9109438/how-to-use-existing-database-with-android-app/9109728#9109728 –

답변

0

귀하의 DB_NAME이 귀하의 assests 폴더에있는 데이터베이스와 같지 않다고 가정합니다.

당신이 reigndesign 다음 경우,이 회선을 점검 : 여기

private static String DB_NAME ="myDBName"; 

DB_NAME은 데이터베이스의 이름입니다. assets 폴더에 데이터베이스 복사본이 있다고 가정합니다. DB_NAME은 assets 폴더에있는 것과 동일해야합니다.

또는

팔로우 this.

+0

내가갔습니다 링크를 통해 그리고 데이터베이스를 복사/체크하는 모든 방법은 지금 내가 가지고있는 것과 비슷합니다. 에뮬레이터에서는 작동합니다. 내 응용 프로그램은 데이터베이스에서 검색 할 때 강제로 닫히지 않습니다. 그러나, 나는 logcat에 (sqlite 반환 된 : 오류 코드 = 1, msg = no such column : table_name) 것으로 나타났습니다. 에뮬레이터에서 원활하게 실행할 수있는 응용 프로그램에도 불구하고 오류가 있다고 생각하십니까? DDMS에서 sqlite 파일을 검사했고 필요한 모든 테이블을 가지고 있습니다. – yurain

+0

내 이해에 따르면, 데이터베이스가 테이블 (EMPTY 데이터베이스)없이 생성되거나 코드에서 잘못된 데이터베이스 이름을 참조하고 있습니다. –

+0

데이터베이스가 존재하는지 확인하십시오. http://stackoverflow.com/questions/9846336/android-how-can-i-view-a-sql-database-created-in-my-app-im-running -it-on-the- –

관련 문제