package com.test.weathertest.db;

import java.util.ArrayList;
import java.util.List;

import com.test.weathertest.model.City;
import com.test.weathertest.model.County;
import com.test.weathertest.model.Province;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class WeatherDB {
	public static final String DB_NAME = "weather_db";
	public static final int VERSION = 1;
	private static WeatherDB weatherDB;
	private SQLiteDatabase db;
	private WeatherDB(Context context){
		WeatherDBOpenHelper dbHelper = new WeatherDBOpenHelper(context, DB_NAME, null, VERSION);
		db = dbHelper.getWritableDatabase();
	}
	public synchronized static WeatherDB getInstance(Context context){
		if(weatherDB==null){
			weatherDB = new WeatherDB(context);
		}
		return weatherDB;
	}
	/*
	 * Provinceʵ洢ݿ
	 * */
	public void saveProvince(Province province){
		if(province!=null){
			ContentValues values = new ContentValues();
			values.put("province_name", province.getProvinceName());
			values.put("province_code", province.getProvinceCode());
			db.insert("Province", null, values);
		}
	}
	/*
	 * ݿȡȫеʡϢ
	 * */
	public List<Province> loadProvinces(){
		List<Province> list = new ArrayList<Province>();
		Cursor cursor = db.query("Province", null, null, null, null, null, null);
		if(cursor.moveToFirst()){
			do{
				Province province = new Province();
				province.setId(cursor.getInt(cursor.getColumnIndex("id")));
				province.setProvinceName(cursor.getString(cursor.getColumnIndex("province_name")));
				province.setProvinceCode(cursor.getString(cursor.getColumnIndex("province_code")));
				list.add(province);
			}while(cursor.moveToNext());
		}
		if(cursor!=null){
			cursor.close();
		}
		return list;
	}
	/*
	 * 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")));
				list.add(city);
			}while(cursor.moveToNext());
		}
		if(cursor!=null){
			cursor.close();
		}
		return list;
	}
	/*
	 * Countyʵ洢ݿ
	 * */
	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<County> loadCounties(int cityId){
		List<County> list = new ArrayList<County>();
		Cursor cursor = db.query("County", null, "city_id=?",new String[]{String.valueOf(cityId)}, null, null, null);
		if(cursor.moveToFirst()){
			do{
				County county = new County();
				county.setId(cursor.getInt(cursor.getColumnIndex("id")));
				county.setCountyName(cursor.getString(cursor.getColumnIndex("county_name")));
				county.setCountyCode(cursor.getString(cursor.getColumnIndex("county_code")));
				list.add(county);
			}while(cursor.moveToNext());
		}
		if(cursor!=null){
			cursor.close();
		}
		return list;
	}
}

@@ -2772,35 +2772,41 @@
 c void saveC
-i
+oun
 ty(C
-i
+oun
 ty c
-i
+oun
 ty)%7B%0A%09%09if(ci
@@ -2796,33 +2796,35 @@
  county)%7B%0A%09%09if(c
-i
+oun
 ty!=null)%7B%0A%09%09%09Co
@@ -2873,33 +2873,35 @@
 %09%09%09values.put(%22c
-i
+oun
 ty_name%22, city.g
@@ -2891,33 +2891,37 @@
 nty_name%22, c
-i
+oun
 ty.getC
-i
+oun
 tyName());%0A%09
@@ -2927,33 +2927,35 @@
 %09%09%09values.put(%22c
-i
+oun
 ty_code%22, city.g
@@ -2945,33 +2945,37 @@
 nty_code%22, c
-i
+oun
 ty.getC
-i
+oun
 tyCode());%0A%09
@@ -2992,38 +2992,32 @@
 ut(%22
-province
+city
 _id%22, c
-i
+oun
 ty.get
-Province
+City
 Id()

add a new DBHelper and all kinds of model2