無法檢索數據庫,並顯示出皮棉警告說:SQLite數據庫硬編碼/數據/ DB_PATH的Android
Do not hardcode "/data/"; use Context.getFilesDir().getPath() instead
是否嘗試過搜索相關錯誤,但沒有什麼幫助。 下面的代碼
private static String DB_PATH = "/data/data/mlearning.fundprog/databases/";
private static String DB_NAME = "questionsDb";
private SQLiteDatabase myDataBase;
private final Context myContext;
public DBHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
DB_PATH = context.getFilesDir().getPath() + context.getPackageName() + DB_NAME;
}
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException{
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
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{
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
我真的不明白什麼是錯。它以前工作,但我認爲這是因爲我已經從API 8移到API 14。我想這個更高的API有編碼的另一個規則。是這樣嗎?
你實際上是否在使用'DB_PATH'? – 2014-08-30 14:31:37
是的,我在@CL使用'DB_PATH'。 – 2014-08-30 14:51:18
不在您顯示的代碼中。 ('SQLiteOpenHelper'構造函數只能看到'DB_NAME'。) – 2014-08-30 15:15:57