SQLite数据库的运用:

Android 中数据的存储,估计平时用得做多的莫过于SharePreference了。因为它简单好用,所以自己项目中用的最多的也是这种。但是对于如下数据的存储。就得依靠数据库了。

SQLite数据库的运用:_第1张图片

所有数据来源于服务端:针对这种数据,可以在splash界面中访问服务端,拿到数据之后,存储在数据库中,后面需要用到数据的地方,从数据库中获取。

1.创建表:

public class RoomTable {

    public final static String TABLE_ROOM = "t_room";

    private final static String ROOM_GROUP = "room_group";
    private final static String ROOM_DETAIL = "room_detail";

    private DBManagerImpl db = null;

    public RoomTable() {
        if (db == null) {
            db = DBManager.get();
        }
        if (!db.isTableExits(db.getConnection(), TABLE_ROOM)) {
            createRoomTable();
        }
    }

    public RoomTable(Context context) {
        if (db == null) {
            db = DBManager.get(context);
        }
        if (!db.isTableExits(db.getConnection(), TABLE_ROOM)) {
            createRoomTable();
        }
    }

    private void createRoomTable() {
        String createSql = "create table if not exists "+TABLE_ROOM+" (id integer primary key autoincrement,"
                + ROOM_GROUP + " varchar,"+ROOM_DETAIL+" varchar)";
        db.creatTable(db.getConnection(), createSql, TABLE_ROOM);
    }


    /*** * * 获取科室详细名称 * 子科室名称:存储规则:科室组名(String),科室子名(String) * @param * @return */
    public ArrayList<String> getRoomDetails(String roomGroup) {
        db = DBManager.get();
        Cursor cursor = null;
        ArrayList<String> roomDetailsList = null;
        try {

            cursor = db.find(db.getConnection(), "select * from " + TABLE_ROOM + " where "+ROOM_GROUP+" = ? ", new String[]{ roomGroup });
            if (cursor != null) {
                roomDetailsList = new ArrayList<String>();
                if(roomGroup.equals("所有科室")){

                    roomDetailsList.add("所有科室");
                }
                while (cursor.moveToNext()) {
                    roomDetailsList.add(cursor.getString(cursor.getColumnIndex(ROOM_DETAIL)));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }
        return roomDetailsList;
    }


    /** * * 获取所有的科室名称 * @return */
    public ArrayList<String> getRoomGroups() {
        db = DBManager.get();
        Cursor cursor = null;
        ArrayList<String> roomGroupslist = null;
        String GROUP_NAME_OTHER = "其他";
        try {
            cursor = db.find(db.getConnection(), "select distinct "+ROOM_GROUP+" from " + TABLE_ROOM , null);
            if (cursor != null) {
                roomGroupslist = new ArrayList<String>();
                roomGroupslist.add("所有科室");
                while (cursor.moveToNext()) {
                    roomGroupslist.add(cursor.getString(cursor.getColumnIndex(ROOM_GROUP)));
                }
                //判断是否包含"其他",如果包含则把"其他"移到最后,如果不包含,则不处理
                boolean hasOther =  false;
                for(String groupName : roomGroupslist){
                    if(GROUP_NAME_OTHER.equals(groupName)){
                        hasOther = true;
                        roomGroupslist.remove(groupName);
                        break;
                    }
                }
                if(hasOther){
                    roomGroupslist.add(GROUP_NAME_OTHER);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }
        return roomGroupslist;
    }

    public void saveRoomGroup(String groupName,String roomDetail) {

        db = DBManager.get();
        ContentValues values = new ContentValues();
        values.put(ROOM_GROUP, groupName);
        values.put(ROOM_DETAIL, roomDetail);
        db.save(db.getConnection(), TABLE_ROOM, values);
    }

    public void clearTable() {

        db = DBManager.get();
        db.delete(db.getConnection(), TABLE_ROOM, null, null);
    }



}

2.访问服务端:获取到数据

 ArrayList<RoomData> roomList= data.getRooms();//获取到科室列表
                                RoomTable roomTable = new RoomTable();
                                roomTable.clearTable();
                                for(RoomData roomData:roomList){
                                    String roomGroup = roomData.getRoom();
                                    ArrayList<String> roomdetailList = roomData.getRoomdetail();
                                    for(String roomDeatialStr:roomdetailList){
                                        roomTable.saveRoomGroup(roomGroup,roomDeatialStr);
                                    }
                                }

另外,需要注意的是:不是每次都要访问服务端获取数据,所以先判断下roomtable中的数据是否存在。只有当数据为空的时候,再去调用。

你可能感兴趣的:(android,sqlite)