我想在sd卡中存储sqlite数据库而不是在本地存储,我在我的代码中尝试了一些例子,但它没有帮助。我添加了我在下面使用过的代码块,请帮助解决我所缺少的问题代码。如果我在这里有一个简单的逻辑,谢谢你和投票。
public class ListSparesRegistrationOpenHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "LIST_List_DB";
public static final String TABLE_NAME = "LIST_List_TABLE";
public static final int VERSION = 1;
public static final String KEY_ID = "_id";
public static final String ID = "ID";
public static final String MATERIAL = "Material";
public static final String SCRIPT = "create table " + TABLE_NAME + " ("
+ KEY_ID + " integer primary key autoincrement, "
+ ID+ " text not null, "
+ MATERIAL + " text not null );";
public ListSparesRegistrationOpenHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(SCRIPT);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("drop table " + TABLE_NAME);
onCreate(db);
}
}
和..
public class ListSparesRegistrationAdapter {
SQLiteDatabase database_ob;
ListSparesRegistrationOpenHelper openHelper_ob;
Context context;
public ListSparesRegistrationAdapter(Context c) {
context = c;
}
public ListSparesRegistrationAdapter opnToRead() {
openHelper_ob = new ListSparesRegistrationOpenHelper(context,
openHelper_ob.DATABASE_NAME, null, openHelper_ob.VERSION);
database_ob = openHelper_ob.getReadableDatabase();
return this;
}
public ListSparesRegistrationAdapter opnToWrite() {
openHelper_ob = new ListSparesRegistrationOpenHelper(context,
openHelper_ob.DATABASE_NAME, null, openHelper_ob.VERSION);
database_ob = openHelper_ob.getWritableDatabase();
return this;
}
public Cursor getAllTitles()
{
String[] cols = { openHelper_ob.KEY_ID, openHelper_ob.ID,
openHelper_ob.MATERIAL};
opnToWrite();
Cursor c = database_ob.query(openHelper_ob.TABLE_NAME, cols,
null, null, null, null, null);
return c;
}
public void Close() {
database_ob.close();
}
public long insertDetails(String id, String material) {
ContentValues contentValues = new ContentValues();
contentValues.put(openHelper_ob.ID, id);
contentValues.put(openHelper_ob.MATERIAL, material);
opnToWrite();
long val = database_ob.insert(openHelper_ob.TABLE_NAME, null,
contentValues);
Close();
return val;
}
public Cursor queryName() {
String[] cols = { openHelper_ob.KEY_ID, openHelper_ob.ID,
openHelper_ob.MATERIAL};
opnToWrite();
Cursor c = database_ob.query(openHelper_ob.TABLE_NAME, cols, null,
null, null, null, null);
return c;
}
public Cursor queryAll(int nameId) {
String[] cols = { openHelper_ob.KEY_ID, openHelper_ob.ID,
openHelper_ob.MATERIAL};
opnToWrite();
Cursor c = database_ob.query(openHelper_ob.TABLE_NAME, cols,
openHelper_ob.KEY_ID + "=" + nameId, null, null, null, null);
return c;
}
public long updateldetail(int rowId, String id, String material) {
ContentValues contentValues = new ContentValues();
contentValues.put(openHelper_ob.ID, id);
contentValues.put(openHelper_ob.MATERIAL, material);
opnToWrite();
long val = database_ob.update(openHelper_ob.TABLE_NAME, contentValues,
openHelper_ob.KEY_ID + "=" + rowId, null);
Close();
return val;
}
public int deletOneRecord(int rowId) {
// TODO Auto-generated method stub
opnToWrite();
int val = database_ob.delete(openHelper_ob.TABLE_NAME,
openHelper_ob.KEY_ID + "=" + rowId, null);
Close();
return val;
}
public int deleteAll(){
opnToWrite();
return database_ob.delete(openHelper_ob.TABLE_NAME, null, null);
}
}
答案 0 :(得分:0)
您可以在构造函数中将路径传递给数据库,例如:
openHelper_ob = new ListSparesRegistrationOpenHelper(context,
openHelper_ob.DATABASE_NAME, null, openHelper_ob.VERSION);
变为
String path = getExternalStorageDirectory() + "myPath/";
openHelper_ob = new ListSparesRegistrationOpenHelper(context,
path + openHelper_ob.DATABASE_NAME, null, openHelper_ob.VERSION);
确保您对SD卡具有R / W权限,并且SD卡均已安装。