SQLiteOpenHelper的问题,完全合格的DB路径名 [英] SQLiteOpenHelper problem with fully qualified DB path name
问题描述
在我的应用程序,我用...
In my app, I use...
myFilesDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/Android/data/" + packageName + "/files");
myFilesDir.mkdirs();
这是罚款,所得的路径是...
This is fine and the resulting path is...
/mnt/sdcard/Android/data/com.mycompany.myApp/files
我需要一个SQLite数据库,我想存储在SD卡上,所以我延长SQLiteOpenHelper如下...
I need a SQLite DB which I want to store on the SD card so I extend SQLiteOpenHelper as follows...
public class myDbHelper extends SQLiteOpenHelper {
public myDbHelper(Context context, String name, CursorFactory factory, int version) {
// NOTE I prefix the full path of my files directory to 'name'
super(context, myFilesDir + "/" + name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
// Create tables and populate with default data...
}
}
到目前为止好 - 我第一次叫 getReadableDatabase()
或 getWriteableDatabase()
空DB是在SD卡上创建和的onCreate()
填充它。
So far so good - the first time I call getReadableDatabase()
or getWriteableDatabase()
the empty DB is created on the SD card and onCreate()
populates it.
因此,这里的问题 - 应用程序是在测试阶段,也许有5或6人,像我一样,他们正在运行的是Android 2.2版,一切工作正常。我有一个测试,但是,在运行V2.1当 myDbHelper
试图在第一次使用创建的数据库,它具有以下崩溃...
So here's the problem - the app is in beta testing with maybe 5 or 6 people and, like me, they're running Android v2.2 and everything works fine. I have one tester, however, running v2.1 and when myDbHelper
tries to create the DB on first use, it crashes with the following...
E/AndroidRuntime( 3941): Caused by: java.lang.IllegalArgumentException: File /nand/Android/data/com.mycompany.myApp/files/myApp-DB.db3 contains a path separator
E/AndroidRuntime( 3941): at android.app.ApplicationContext.makeFilename(ApplicationContext.java:1445)
E/AndroidRuntime( 3941): at android.app.ApplicationContext.openOrCreateDatabase(ApplicationContext.java:473)
E/AndroidRuntime( 3941): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:193)
E/AndroidRuntime( 3941): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
E/AndroidRuntime( 3941): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:158)
有关的文件目录的路径是一个奇数(/ NAND),因为它的内存,虽然不是手机本身的内存 - 但它是)由 getExternalStorageDirectory(返回的路径
此设备。
The path for the files directory is an odd one ("/nand") as it's internal memory although not the phone's own internal memory - but it is the path returned by getExternalStorageDirectory()
for this device.
我可以看到三种可能的答案...
I can see three possible answers...
- 虽然可以接受的V2.2,指定为DB名称完全合法的路径是不建议将无法在早期版本
- 完全合格的路径是可以接受的SD卡存储,但/ NAND路径正在被PTED为内部,只有相对路径间$ P $在这种情况下,可以接受
- 在别的东西,我很想念完全
如果上述任何或所有申请我最好的AP preciate,如果有人可以帮助我应该如何处理这一点。
If any or all of the above apply I'd appreciate it if somebody could help with how I should approach this.
感谢。
推荐答案
从历史上看,你一直无法使用路径与 SQLiteOpenHelper
。它仅制作简单的文件名。我没有意识到,他们放松了这个限制在Android 2.2的。
Historically, you have not been able to use paths with SQLiteOpenHelper
. It only worked on simple filenames. I had not realized that they relaxed that restriction in Android 2.2.
如果你希望使用的数据库在SD卡上,和你想支持的Android 2.1和更早的版本,则不能使用 SQLiteOpenHelper
。
If you wish to use databases on the SD card, and you wish to support Android 2.1 and earlier, you cannot use SQLiteOpenHelper
.
抱歉!
这篇关于SQLiteOpenHelper的问题,完全合格的DB路径名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!