/** 将City实例存储到数据库。 */ public void saveCity(City city) { if (city != null) { ContentValues values = new ContentValues(); values.put("city_name", city.getCityName()); values.put("city_code", city.getCityCode()); values.put("province_id", city.getProvinceId()); db.insert("City", null, values); } }
/** 从数据库读取某省下所有的城市信息。 */ public List<City> loadCities(int provinceId) { List<City> list = new ArrayList<City>(); Cursor cursor = db.query( "City", null, "province_id = ?", new String[] {String.valueOf(provinceId)}, null, null, null); if (cursor.moveToFirst()) { do { City city = new City(); city.setId(cursor.getInt(cursor.getColumnIndex("id"))); city.setCityName(cursor.getString(cursor.getColumnIndex("city_name"))); city.setCityCode(cursor.getString(cursor.getColumnIndex("city_code"))); city.setProvinceId(provinceId); list.add(city); } while (cursor.moveToNext()); } return list; }