장치의 데이터 폴더에 액세스하려면 루트 폴더에 있어야합니다. 그러나 자산 폴더에서 데이터베이스의 데이터 폴더로 데이터베이스를 복사하려는 경우 복사되지 않은 전화에서 복사 프로세스가 작동합니까? 다음은 데이터베이스 도우미 클래스입니다. 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) {
}
}
는 소스 코드와 함께,이 완전한/작업 자습서를보십시오 : 가 http://stackoverflow.com/questions/9109438/how-to-use-existing-database-with-android-app/9109728#9109728 –