user3606357
user3606357

Reputation: 11

SQLite Android: Custom database directory creation; File contains a path seperator

Is it possible in SQLite to place the database files on a seperate folder inside of yourpackagename instead of yourpackagename/databases?

For example, I pass this string to the constructor of my SQLiteOpenHelper:

    data/data/com.example.app/appbooks/App Book 1.db

Also tried doing this as the string, same result:

    dbName = "data" + File.separator + "data" + File.separator + "com.example.app" + File.separator + "appbooks" + File.separator + dbName + ".db";

After that, I receive the an error in logcat at execution that the string above contains a path separator. I tried doing the usual slashes and replacing those slashes with File.separator, but the error still persists.

    05-06 21:23:25.896: E/AndroidRuntime(5214): Caused by: java.lang.IllegalArgumentException: File data/data/com.example.app/appbooks/App Book 1.db contains a path separator

Upvotes: 1

Views: 1073

Answers (1)

CL.
CL.

Reputation: 180260

Your databases must be in the directory returned by getDatabasePath().

If you really want to have an hierarchy of database file names, replace the path separator with any other character. (In Linux, file names can contain any character except /.) If you want to be particularly evil, use a backslash:

final String myFakeSeparator = "\\";
...
dbName = ... + File.separator + "appbooks" + myFakeSeparator + dbName + ".db";

Upvotes: 1

Related Questions