SQLite 데이터베이스가 있고 Android 앱에서 사용하고 싶습니다.Android 및 SQLite, 데이터베이스를 읽는 장치의 오류
private void copyDBFromAsssetsToDataFolder()
{
try
{
String destPath = "/data/data/" + getPackageName() + "/databases/dbname.db";
File f = new File(destPath);
if(!f.exists())
{
Log.v("TAG","File Not Exist");
InputStream in = getAssets().open("dbname.db");
OutputStream out = new FileOutputStream(destPath);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0)
{
out.write(buffer, 0, length);
}
in.close();
out.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
Log.v("TAG","ioexeption");
e.printStackTrace();
}}
내 모든 쿼리를 시뮬레이터에서 잘 작동하지만 장치 나 수신 해요 : :
오류를
우선 내가 사용// 데이터/데이터 // 데이터베이스에 자산에서 내 데이터베이스를 복사/data/data/packagename/databases/
여기에서 나는 동일한 문제가있는 사람들을 읽으며 솔루션은 항상이 디렉터리에 사용 권한을 할당하거나 데이터베이스를 외부 저장소에 복사합니다. 그러나 프로덕션 환경에서 사용자는 SD 카드를 보유 할 필요가 없으며 데이터베이스 (또는 기타) 디렉토리에 사용 권한을 할당 할 수 없습니다.
이미 생성 된 데이터베이스를 사용하는 가장 좋은 해결책은 무엇입니까?
하드 코딩 된 파일 경로 대신 Context.getDatabasePath ("name_of_db");'를 사용하지 마십시오. 그러면 파일이 file.mkdirs();에없는 경우 copy 자산에서? – Selvin
비슷한 질문을했습니다. 유용한 참고 자료 : http://stackoverflow.com/questions/513084/how-to-ship-an-android-application-with-a-database 및 http://stackoverflow.com/questions/9109438/how-to- 안드로이드 애플리케이션을 사용하는 기존 데이터베이스 사용 –
데이터베이스 관리에 도움이되는 패키지가 작성되었습니다. https://github.com/jgilfelt/android-sqlite-asset-helper –