如何使用SQLiteOpenHelper与SD卡的数据库? [英] How using SQLiteOpenHelper with database on sd-card?
问题描述
这是不是为我工作。建议的结构依然采用内部存储器中的数据库。其实方法getDatabasePath()它永远不会被SQLiteOpenHelper。
我希望得到这个启动和运行。
下面是我所做的,到目前为止:
1)扩展应用:
公共类MyApplication的扩展应用{
@覆盖
公共文件getDatabasePath(字符串名称){
//只是一个测试
文件file = super.getDatabasePath(名称);
返回的文件;
}
@覆盖
公共无效的onCreate(){
//只是一个测试
super.onCreate();
}
}
2)添加扩展应用到清单:
<应用
...
机器人:名称=所有MyApplication
...>
3)扩展,并使用SQLiteOpenHelper:
公共类MySqliteOpenHelper扩展SQLiteOpenHelper {
公共无效的onCreate(SQLiteDatabase sqliteDatabase){
...
}
@覆盖
公共无效onUpgrade(SQLiteDatabase sqliteDatabase,诠释oldVersion,诠释静态网页){
...
}
}
4)以通常的方式用在我的活动扩展SQLiteOpenHelper:
公共类MyActivity延伸活动{
私人MySqliteOpenHelper mySqliteOpenHelper;
私人SQLiteDatabase sqliteDatabase;
@覆盖
公共无效的onCreate(束捆){
super.onCreate(包);
...
mySqliteOpenHelper =新MySqliteOpenHelper(getApplicationContext());
sqliteDatabase = mySqliteOpenHelper.getReadableDatabase();
...
}
@覆盖
保护无效的onDestroy(){
如果(mySqliteOpenHelper!= NULL){
mySqliteOpenHelper.close();
mySqliteOpenHelper = NULL;
}
super.onDestroy();
}
}
我想指出的是,扩展的应用程序类的工作一般。我可以看到这一点,因为MyApplication.onCreate()被调用。但MyApplication.getDatabasePath()将不会调用。
任何帮助是非常AP preciated。
我发现我可以在Android 2.2的使用完整的路径,但在2.1 Context.openOrCreateDatabase()方法引发了异常。要解决此我包的方法()直接调用SQLiteDatabase.openOrCreateDatabase。下面是构造我的扩展SQLOpenHelper
公共类数据库扩展SQLiteOpenHelper {
公共数据库(上下文的背景下){
超(新ContextWrapper(上下文){
@覆盖公共SQLiteDatabase openOrCreateDatabase(字符串名称,
INT模式,SQLiteDatabase.CursorFactory厂){
//指定允许数据库目录
文件DIR =新的文件(DIR);
如果(!dir.exists()){
dir.mkdirs();
}
返回SQLiteDatabase.openDatabase(DIR +/+姓名,空,
SQLiteDatabase.CREATE_IF_NECESSARY);
}
},名称,空,VERSION);
this.context =背景;
}
}
According to various answers here and in the web extending Application and it's inherited method getDatabasePath() would allow to set the database storage path from the standard internal memory location to an inserted SD-card of bigger size.
This is not working for me. The suggested construct is still using the database on internal memory. In fact the method getDatabasePath() is never called by SQLiteOpenHelper.
I would like to get this up and running.
Here's what I did so far:
1.) Extending Application:
public class MyApplication extends Application {
@Override
public File getDatabasePath(String name) {
// Just a test
File file = super.getDatabasePath(name);
return file;
}
@Override
public void onCreate() {
// Just a test
super.onCreate();
}
}
2.) Adding extended Application to the Manifest:
<application
...
android:name="MyApplication"
... >
3.) Extending and using SQLiteOpenHelper:
public class MySqliteOpenHelper extends SQLiteOpenHelper {
public void onCreate(SQLiteDatabase sqliteDatabase) {
...
}
@Override
public void onUpgrade(SQLiteDatabase sqliteDatabase, int oldVersion, int newVersion) {
...
}
}
4.) Using the extended SQLiteOpenHelper in my Activities in the usual way:
public class MyActivity extends Activity {
private MySqliteOpenHelper mySqliteOpenHelper;
private SQLiteDatabase sqliteDatabase;
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
...
mySqliteOpenHelper = new MySqliteOpenHelper(getApplicationContext());
sqliteDatabase = mySqliteOpenHelper.getReadableDatabase();
...
}
@Override
protected void onDestroy() {
if (mySqliteOpenHelper != null) {
mySqliteOpenHelper.close();
mySqliteOpenHelper = null;
}
super.onDestroy();
}
}
I want to point out that the extended Application class is working in general. I can see this because MyApplication.onCreate() is called. But MyApplication.getDatabasePath() is not called.
Any help is highly appreciated.
I found I could use a full path in Android 2.2, but in 2.1 the Context.openOrCreateDatabase() method threw an exception. To work around this I wrapped that method to call SQLiteDatabase.openOrCreateDatabase() directly. Here is the constructor for my extended SQLOpenHelper
public class Database extends SQLiteOpenHelper {
public Database(Context context) {
super(new ContextWrapper(context) {
@Override public SQLiteDatabase openOrCreateDatabase(String name,
int mode, SQLiteDatabase.CursorFactory factory) {
// allow database directory to be specified
File dir = new File(DIR);
if(!dir.exists()) {
dir.mkdirs();
}
return SQLiteDatabase.openDatabase(DIR + "/" + NAME, null,
SQLiteDatabase.CREATE_IF_NECESSARY);
}
}, NAME, null, VERSION);
this.context = context;
}
}
这篇关于如何使用SQLiteOpenHelper与SD卡的数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!