0

Here is the code to connect the database named "library.db" in the assets file.

public abstract class DBConstant {
    //database file directory
    public static String DATABASE_PATH = "/data/data/(pack_name)/databases";
    //database file name
    public static String DATABASE_FILE = "library.db";
    //database version
    public static int DATABASE_VERSION = 1;
}

However, in my laptop, the laptop with windows or the laptop with mac os, the app cannot find the .db file. After edit the /databases to /assets, all goes well. Another weird is that when I edit it back to /databases, it goes well on my laptop with windows, but become failed on other laptops.

What can that happen? There seems no difference in other codes.

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
HaoyuChen
  • 1
  • 1
  • 1
    do you `clean` and `compile` properly ? – Ravi Sep 28 '17 at 04:14
  • ok, may that work. I found clean project, run on the emulator is compile, right? I am new in the android, sorry. – HaoyuChen Sep 28 '17 at 04:41
  • Why are you using an explicit path? You should use [SQLiteOpenHelper](https://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html), or [getDatabasePath()](https://developer.android.com/reference/android/content/Context.html#getDatabasePath(java.lang.String)). – CL. Sep 28 '17 at 06:53

1 Answers1

1

On compile it suppose to convert to the right separator I believe, but platform independent would look something like this:

String path = File.separator + "data"+ File.separator + "data" + File.separator + "pack_name" + File.separator + "databases";

see Questions: File.separator or File.pathSeparator and Why is File.separator in Android using? and Difference between File.separator and slash in paths

ziondreamt
  • 467
  • 1
  • 4
  • 14
  • Actually, I don't think it is the reason, but the knowledge is useful in the future programming. Thx anyway. – HaoyuChen Sep 28 '17 at 05:05