如何正确写大CRUD sqlite android应用
我正在写一个应用程序,操纵数据库由3个表组成。我使用模型(Worker模型,特殊模型)和getter和setter从json文件创建了这个数据库。现在我想从这个数据库中获得特定的信息。我已经做到了,但我的代码非常愚蠢。我需要的是改变我的应用程序的架构,但我不知道它应该是什么样子。 这是我的方法的例子,他们很漂亮的一周 这是我如何将信息添加到数据库。 ,我喜欢它,我认为这是正确的:如何正确写大CRUD sqlite android应用
public void addWorker(Worker worker){ List<Specialty> specialty;
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
//fixind names
String f_name = fixName(worker.getF_name());
String l_name = fixName(worker.getL_name());
String birthday = fixDate(worker.getBirthday());
values.put(KEY_F_NAME, f_name);
values.put(KEY_L_NAME, l_name);
values.put(KEY_BIRTHDAY, birthday);
values.put(KEY_AVATR_URL, worker.getAvart_url());
specialty = worker.getSpecialty();
long worker_id = db.insert(TABLE_WORKERS,null,values);
//add unique specialty
for (Specialty spec: specialty){
createRelations(worker_id, spec.getSpecialty_id());
if (getCount(spec.getSpecialty_id()) == 0){
addSpecialty(spec);
}
}
}
这就是我如何利用信息从数据库中: 公众的String [] getFullInfo(字符串WORKER_NAME){ 字符串selectQuery = “巨额QUERY HERE”,其中workers.f_name ?=“; Log.e(LOG_TAG,selectQuery);
SQLiteDatabase db = this.getReadableDatabase(); Cursor c = db.rawQuery(selectQuery, new String [] {worker_name});
String[] details = new String[5];
c.moveToFirst();
while (c.isAfterLast() == false){
details[0] = c.getString(c.getColumnIndex(KEY_F_NAME));
details[1] = c.getString(c.getColumnIndex(KEY_L_NAME));
details[2] = c.getString(c.getColumnIndex(KEY_BIRTHDAY));
details[3] = c.getString(c.getColumnIndex("age"));
details[4] = c.getString(c.getColumnIndex(KEY_SPEC_NAME));
c.moveToNext();
}
return details;
}
这是我的另一个查询,它有不同的返回类型:
public List<Map<String ,String>> getWorkerListBySpec(String spec_name){ String selectQuery = "HUGE QUERY HERE
"where specialty.spec_name=?";
Log.e(LOG_TAG, selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, new String [] {spec_name});
//making list of workers
List<Map<String ,String>> data = new ArrayList<Map<String,String>>();
c.moveToFirst();
while (c.isAfterLast() == false){
Map<String,String> datum = new HashMap<String,String>(2);
datum.put(KEY_F_NAME, c.getString(c.getColumnIndex(KEY_F_NAME)));
datum.put(KEY_L_NAME, c.getString(c.getColumnIndex(KEY_L_NAME)));
datum.put(KEY_BIRTHDAY, c.getString(c.getColumnIndex(KEY_BIRTHDAY)));
data.add(datum);
c.moveToNext();
}
return data;
}
和第三个,它们也有不同的回报类型:
public List<String> getAllSpecs_Names(){ List<String> spec_names = new ArrayList<String>();
String selectQuery = "select * from " + TABLE_SPECIALTY;
Log.e(LOG_TAG, selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
if (c.moveToFirst()){
do{
spec_names.add(c.getString(c.getColumnIndex(KEY_SPEC_NAME)));
}while (c.moveToNext());
}
return spec_names;
}
我知道,这都是错的。 请告诉我,我应该怎么做我的所有疑问。 这将是很好的,如果你给我检查应用程序看起来应该像
回答:
,而不是返回一个列表或地图的链接,你应该更好地返回自己的CursorWrapper
,所以你可以可能看起来像这样的:
public WorkerCursor getWorkerListBySpec(String spec_name){ String selectQuery = "HUGE QUERY HERE
"where specialty.spec_name=?";
Log.e(LOG_TAG, selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, new String [] {spec_name});
return new WorkerCursor(cursor);
}
public static class WorkerCursor extends CursorWrapper {
/**
* Creates a cursor wrapper.
*
* @param cursor The underlying cursor to wrap.
*/
public WorkerCursor(Cursor cursor) {
super(cursor);
}
public Worker getWorker() {
return getWorkerAtCursor();
}
public Worker getWorker(int position) {
if (moveToPosition(position)) {
return getWorkerCursor();
} else {
return null;
}
}
private Worker getWorkerAtCursor() {
if (isBeforeFirst() || isAfterLast()) {
return null;
}
worker worker = new Worker();
worker.name = c.getString(c.getColumnIndex(KEY_F_NAME));
....
return worker;
}
}
与同为你的列表,只需用其他CursorWrapper
,不要忘记关闭CursorWrapper,当它不再需要,LIK e onDestroy of Activity等
以上是 如何正确写大CRUD sqlite android应用 的全部内容, 来源链接: utcz.com/qa/259882.html