/*从数据库读取某省下所有的城市*/ public List<Country> loadCountries(int cityId) { List<Country> list = new ArrayList<Country>(); Cursor cursor = db.query( "Country", null, "city_id = ?", new String[] {String.valueOf(cityId)}, null, null, null); if (cursor.moveToFirst()) { do { Country country = new Country(); country.setId(cursor.getInt(cursor.getColumnIndex("id"))); country.setCountryname(cursor.getString(cursor.getColumnIndex("country_name"))); country.setCountrycode(cursor.getString(cursor.getColumnIndex("country_code"))); country.setCityid(cityId); list.add(country); } while (cursor.moveToNext()); } if (cursor != null) { cursor.close(); } return list; }
/*将country数据存储到数据库*/ public void savecountry(Country country) { if (country != null) { ContentValues val = new ContentValues(); val.put("country_name", country.getCountryname()); val.put("country_code", country.getCountrycode()); val.put("city_id", country.getCityid()); db.insert("Country", null, val); } }