1

Cannot retrieve database, and shows a lint warning that says:

Do not hardcode "/data/"; use Context.getFilesDir().getPath() instead

Have tried searching for related errors but nothing helps. Here's the code

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

I really dont get what's wrong with this. It was working before, but i'm thinking it's because i have moved to API 14 from API 8. I guess this higher API has another rule for codings. Is that so?

5
  • Are you actually using DB_PATH anywhere? Commented Aug 30, 2014 at 14:31
  • Yes, Im using DB_PATH everywhere @CL. Commented Aug 30, 2014 at 14:51
  • Not in the code you've shown. (The SQLiteOpenHelper constructor sees only DB_NAME.) Commented Aug 30, 2014 at 15:15
  • I've added more code in the post @CL Commented Aug 30, 2014 at 16:06
  • Where did you get this code from? Better use SQLiteAssetHelper. Commented Aug 30, 2014 at 17:48

2 Answers 2

3

To get the path to a database, use getDatabasePath().

Sign up to request clarification or add additional context in comments.

Comments

1

You can use any of the below 2 methods to get the path of your database:

  • DB_PATH = "/data/data/" + context.getApplicationContext().getPackageName() + "/databases";


or use this

  • DB_PATH = context.getDatabasePath(DB_NAME).getParent();

3 Comments

What does v in v.getDatabasePath stands for?
That should be placed inside the DBHelper constructor right? I've tried it and still doesn't work
Yes. Try debugfing ur app and see waht contains DB PATH

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.