如何正确编写大CRUD sqlite android app

时间:2015-06-11 09:15:48

标签: android sqlite

我正在编写一个用数据库操作的应用程序,包含3个表。我使用带有getter和setter的模型(Worker模型,专业模型)从json文件创建了这个数据库。现在我想从这个数据库中获取具体信息。我已经做到了,但我的代码非常愚蠢。我需要的是改变我的应用程序的架构,但我不知道它应该是什么样子。 这是我的方法的例子,它们是非常好的一周 这是我如何将信息添加到数据库中。我喜欢它,我认为这是正确的:

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);
        }

    }
}

这就是我从数据库中获取信息的方式:     public String [] getFullInfo(String worker_name){         String selectQuery =“HUGE QUERY HERE”where 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;
}

我知道,这都是错的。 请告诉我如何提出所有疑问。 如果你给我链接来检查应用程序应该是什么样子

会很好

1 个答案:

答案 0 :(得分:0)

而不是返回List或Map,你应该最好返回一个自己的 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;
}
}

和你的List一样,只是使用其他CursorWrapper

并且不要忘记关闭CursorWrapper,当它不再需要时,就像onDestroy of Activity那样