我正在尝试将我的sqlite数据库保存到SD卡。我尝试了一些事情,但我无法弄清楚,有些人给了我错误,例如它虽然创建了它但找不到表SD卡上的数据库。 我最终删除了所有内容......
我需要添加或更改以保存到SD卡? 为了说清楚,我不是要把它复制到SD卡,我试图把它保存在SD卡而不是内部存储器上。
到目前为止,这是我的数据库。
public class Backup_DB extends SQLiteOpenHelper {
private static final String DB_NAME = "backup.db";
private static final int DB_VERSION_NUMBER = 1;
private static final String DB_TABLE_NAME = "backup";
private static final String TAG = "CountriesDbAdapter";
private final Context mCtx;
private Backup_DB mDbHelper;
public static final String KEY_ROWID = "_id";
static final String DB_COLUMN_1_NAME = "country_name";
static final String DB_COLUMN_3_NAME = "country_price";
private static final String DB_CREATE_SCRIPT = "create table "
+ DB_TABLE_NAME
+ " ( _id INTEGER PRIMARY KEY AUTOINCREMENT,country_name text unique,country_price REAL)";
private SQLiteDatabase sqliteDBbackup = null;
public Backup_DB(Context context) {
super(context, DB_NAME, null, DB_VERSION_NUMBER);
this.mCtx = context;
}
public Backup_DB open() throws SQLException {
mDbHelper = new Backup_DB(mCtx);
sqliteDBbackup = mDbHelper.getWritableDatabase();
return this;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO: Implement onUpgrade
}
@Override
public void onCreate(SQLiteDatabase sqliteDBbackup) {
Log.i("onCreate", "Creating the database...");
sqliteDBbackup.execSQL(DB_CREATE_SCRIPT);
}
public void openDB() throws SQLException {
Log.i("openDB", "Checking sqliteDBbackup...");
if (this.sqliteDBbackup == null) {
Log.i("openDB", "Creating sqliteDBbackup...");
this.sqliteDBbackup = this.getWritableDatabase();
}
}
public void closeDB() {
if (this.sqliteDBbackup != null) {
if (this.sqliteDBbackup.isOpen())
this.sqliteDBbackup.close();
}
}
public void insertCountry(String countryName, String countryPrice) {
sqliteDBbackup.execSQL("INSERT OR IGNORE INTO " + DB_TABLE_NAME
+ "(country_name) VALUES('" + countryName + "')");
sqliteDBbackup.execSQL("UPDATE " + DB_TABLE_NAME
+ " SET country_price=" + countryPrice
+ " WHERE country_name='" + countryName + "';");
}
答案 0 :(得分:1)
SQLiteOpenHelper不是为创建外部数据库而构建的。在SQLiteOpenHelper构造函数中,传入上下文,它用于在包文件夹中创建文件。看一下源代码SQLiteOpenHelper。但是,您可以先在内部创建,然后再将其导出。请记住添加权限。
尝试我写的DBAssetHelper
这个助手类导出:
new AssetDatabaseHelper(this,"test.sqlite").exportDatabase("/flder_toExport/canBerenamedhere.sqlite");