gpt4 book ai didi

android - 是否可以将内部数据库移动到 SDCard?

转载 作者:IT王子 更新时间:2023-10-29 06:22:32 25 4
gpt4 key购买 nike

随着我应用中数据库的增长,它需要越来越多的内部手机空间。数据库中没有任何敏感/隐私数据,因此我有兴趣将其移至 SD 卡。

我正在使用 SQLiteOpenHelper 来协助数据库工作。据我了解,您不能将其用于 SD 卡上的数据库访问,因为您无法定义数据库路径。但是,Internet 上有一些(非常糟糕的)示例建议您可以覆盖此限制。但是,我从来没有编译过这些代码示例之一。

这可能吗?如果是这样——如何!请注意,Froyo 的“SD 卡上的应用程序”功能将不起作用,因为它不会移动内部文件。

最佳答案

只需使用:

SQLiteDatabase.openDatabase(DB_FULL_PATH, null, SQLiteDatabase.OPEN_READONLY);

DB_FULL_PATH 可以是你的 sdcard 的路径,比如/sdcard/mydatabase.db

编辑:

这就是我在我的应用程序中调用的访问数据库的方法....

private static DBUtil dbHelper = null;

public void openDatabase() {
if(dbHelper == null) {
dbHelper = new DBUtil(this.context);
dbHelper.openDataBase(SQLiteDatabase.OPEN_READWRITE);
}
}
public void closeDatabase() {
if(dbHelper != null) {
dbHelper.close();
dbHelper = null;
}
}

...这是我正在使用的数据库助手类,它实际上扩展了 SQLiteOpenHelper,因此您仍然可以从该类中获得所需的一切。

package com.myapp.android.db;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import com.myapp.android.MyApp;

import java.io.IOException;

/**
* Standard database utility class.
*
* TODO: Refactor.
*/
public class DBUtil extends SQLiteOpenHelper {

/**
* Database directory.
*
* <p>
* Example: "/sdcard/myapp/db/"
* </p>
*/
public static String DB_DIRECTORY = null;

/**
* Name of the database file.
*
* <p>
* Example: "mydatabase.db"
* </p>
*
*/
public static String DB_NAME = null;

/**
* Full absolute path of the database.
*
* <p>
* Example: "/sdcard/myapp/db/mydatabase.db"
* </p>
*/
public static String DB_FULL_PATH = null;
static {
DB_DIRECTORY = MyApp.DATA_REPOSITORY_URI + "/myapp/db/";
DB_NAME = "mydatabase.db";
DB_FULL_PATH = DB_DIRECTORY + DB_NAME;
}

private SQLiteDatabase myDataBase;

/**
* Constructor Takes and keeps a reference of the passed context in order to
* access to the application assets and resources.
*
* @param context
*/
public DBUtil(Context context) {
super(context, DB_NAME, null, 1);

try {
this.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
}

/**
* Creates a empty database on the system and rewrites it with your own
* database.
* */
public void createDataBase() throws IOException {
if (!checkDataBase()) this.getWritableDatabase();
}

/**
* Check if the database already exist to avoid re-copying the file each
* time you open the application.
*
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
checkDB = SQLiteDatabase.openDatabase(DB_FULL_PATH, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
// database does't exist yet.
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}

public void openDataBase(int mode) throws SQLException {
try {
myDataBase = SQLiteDatabase.openDatabase(DB_FULL_PATH, null, mode);
} catch(IllegalStateException e) {
// Sometimes, esp. after application upgrade, the database will be non-closed, raising a IllegalStateException
// below. Try to avoid by simply opening it again.
Log.d(MyApp.APP, "Database non-closed. Reopening.");
myDataBase = SQLiteDatabase.openDatabase(DB_FULL_PATH, null, mode);
}
}

public void openDataBase() throws SQLException {
openDataBase(SQLiteDatabase.OPEN_READWRITE);
}

public SQLiteDatabase getDb() {
return myDataBase;
}

@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}

@Override
public void onCreate(SQLiteDatabase db) {
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}

}

关于android - 是否可以将内部数据库移动到 SDCard?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3436434/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com