本文介绍了如何存储在SD卡的Android SQLite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚刚创建的数据库,它通过default.Now存储在数据/数据/数据库文件夹我想在SD卡存储数据库中,我使用下面的code.手电筒建议我在哪里都添加$ C $下that.Thanks
code:
公共类数据库处理器{
SQLiteDatabase数据库;
私有静态数据库处理器OBJ;
私人最终字符串TABLE_NAME =main_db;
私人最终字符串COLUMN_FIRST =_id;
MovieDetails MD =新MovieDetails();
私人数据库处理器(上下文的背景下){
数据库dbobj =新的数据库(背景下,Id_db.db,NULL,1);
数据库= dbobj.getWritableDatabase();
}
公共静态数据库处理器的getInstance(上下文的背景下){
如果(OBJ == NULL){
OBJ =新的数据库处理器(上下文);
}
返回OBJ;
}
公共无效insertData(字符串ID){
尝试 {
ContentValues值=新ContentValues();
values.put(COLUMN_FIRST,ID);
database.insert(TABLE_NAME,空,价值观);
}赶上(例外ER){
Log.d(错误是===,er.toString());
}
}
公共无效deleteData(字符串ID){
database.delete(TABLE_NAME,BaseColumns._ID +=+ ID,NULL);
}
公共光标的getData(){
的String []列= {BaseColumns._ID,COLUMN_FIRST};
回数据库
.query(TABLE_NAME,列,NULL,NULL,NULL,NULL,NULL);
}
私有类数据库扩展SQLiteOpenHelper {
公共数据库(上下文的背景下,字符串名称,CursorFactory厂,
INT版){
超(背景下,名称,厂家,版本);
}
@覆盖
公共无效的onCreate(SQLiteDatabase DB){
db.execSQL(CREATE TABLE+ TABLE_NAME +(+ BaseColumns._ID
+INTEGER PRIMARY KEY AUTOINCREMENT,+ COLUMN_FIRST
+VARCHAR(50)NOT NULL UNIQUE););
}
@覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
}
}
}
解决方案
使用这个类来复制你的数据库:
公共类MyDatabaseTools {
私人字符串的appName =;
私人字符串的packageName =;
公共布尔备份(){
布尔RC = FALSE;
布尔可写= isSDCardWriteable();
如果(写){
档案文件=新的文件(Environment.getDataDirectory()+/数据/+的packageName +/数据库/+ MySQLiteOpenHelper.getDatabaseName());
文件fileBackupDir =新的文件(Environment.getExternalStorageDirectory()的appName +/备份);
如果(!fileBackupDir.exists()){
fileBackupDir.mkdirs();
}
如果(file.exists()){
文件文件备份=新的文件(fileBackupDir,MySQLiteOpenHelper.getDatabaseName());
尝试 {
fileBackup.createNewFile();
FileUtils.copyFile(文件,文件备份);
RC = TRUE;
}赶上(IOException异常IOException异常){
//
}赶上(例外的例外){
//
}
}
}
返回RC;
}
私人布尔isSDCardWriteable(){
布尔RC = FALSE;
字符串状态= Environment.getExternalStorageState();
如果(Environment.MEDIA_MOUNTED.equals(州)){
RC = TRUE;
}
返回RC;
}
公共MyDatabaseTools(最终上下文的背景下,最终字符串的appName){
this.appName =的appName;
的packageName = context.getPackageName();
}
}
请参考,如果你不知道如何使用它的来源。
I have just created database,its stored in data/data/database folder by default.Now i wanna store database in sdcard,I have used following code.please suggest me where have to add code for that.Thanks
Code:
public class DataBaseHandler {
SQLiteDatabase database;
private static DataBaseHandler obj;
private final String TABLE_NAME = "main_db";
private final String COLUMN_FIRST = "_id";
MovieDetails md = new MovieDetails();
private DataBaseHandler(Context context) {
DataBase dbobj = new DataBase(context, "Id_db.db", null, 1);
database = dbobj.getWritableDatabase();
}
public static DataBaseHandler getinstance(Context context) {
if (obj == null) {
obj = new DataBaseHandler(context);
}
return obj;
}
public void insertData(String id) {
try {
ContentValues values = new ContentValues();
values.put(COLUMN_FIRST, id);
database.insert(TABLE_NAME, null, values);
} catch (Exception er) {
Log.d("Error is===", er.toString());
}
}
public void deleteData(String id) {
database.delete(TABLE_NAME, BaseColumns._ID + "=" + id, null);
}
public Cursor getData() {
String[] columns = { BaseColumns._ID, COLUMN_FIRST };
return database
.query(TABLE_NAME, columns, null, null, null, null, null);
}
private class DataBase extends SQLiteOpenHelper {
public DataBase(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + "(" + BaseColumns._ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_FIRST
+ " varchar(50)NOT NULL UNIQUE);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
}
解决方案
Use this class to copy your DB:
public class MyDatabaseTools {
private String appName = "";
private String packageName = "";
public boolean backup() {
boolean rc = false;
boolean writeable = isSDCardWriteable();
if (writeable) {
File file = new File(Environment.getDataDirectory() + "/data/" + packageName + "/databases/" + MySQLiteOpenHelper.getDatabaseName());
File fileBackupDir = new File(Environment.getExternalStorageDirectory(), appName + "/backup");
if (!fileBackupDir.exists()) {
fileBackupDir.mkdirs();
}
if (file.exists()) {
File fileBackup = new File(fileBackupDir, MySQLiteOpenHelper.getDatabaseName());
try {
fileBackup.createNewFile();
FileUtils.copyFile(file, fileBackup);
rc = true;
} catch (IOException ioException) {
//
} catch (Exception exception) {
//
}
}
}
return rc;
}
private boolean isSDCardWriteable() {
boolean rc = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
rc = true;
}
return rc;
}
public MyDatabaseTools(final Context context, final String appName) {
this.appName = appName;
packageName = context.getPackageName();
}
}
Please refer to the Source if you dont understand how to use it.
这篇关于如何存储在SD卡的Android SQLite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!