第一阶段我们要做的就是创建数据库和表,这样服务器获取到的数据才能保存到本地,province,city,county这三张表,
package com.coolweather.app.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class CoolWeatherOpenHelper extends SQLiteOpenHelper{
String TAG = "CoolWeatherOpenHelper";
/**
* Province建表语句
*/
public static final String CREATE_PROVINCE = "create table Province("
+ "id integer primary key autoincrement,"//自增长主键
+ "province_name text,"//省名
+ "province_code text)";//省级代号
/**
* City建表语句
*/
public static final String CREATE_CITY = "create table City("
+ "id integer primary key autoincrement,"//自主增长键
+ "city_name text,"//城市名
+ "city_code text,"//市级代号
+ "province_id integer)";//City表关联Province表的外键
/**
* County 表建表语句
*/
public static final String CREATE_COUNTY = "create table County("
+ "id integer primary key autoincrement,"//自主增长键
+ "county_name text,"//县名
+ "county_code text,"//县级代号
+ "city_id integer)";//County表链City表的外键
public CoolWeatherOpenHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_PROVINCE);
Log.i(TAG, "province created");
db.execSQL(CREATE_CITY);
Log.i(TAG, "city created");
db.execSQL(CREATE_COUNTY);
Log.i(TAG, "county created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
上面的代码很简单,也很容易了解,三条建表语句,然后再oncreate方法里面执行。
然后,很正常的我们就应该想到应该建立几个实体类了,方便后续的开发工作,因此,在model包下建立Province类
package com.coolweather.app.model;
/**
* province的实体类,方便对province的操作
* @author aiyuan
* @time 2016.9.7
*/
public class Province {
private int id;
private String provinceName;
private String provinceCode;
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
public String getProvinceName(){
return provinceName;
}
public void setProvinceName(String provinceName){
this.provinceName = provinceName;
}
public String getProvinceCode(){
return provinceCode;
}
public void setProvinceCode(String provinceCode){
this.provinceCode = provinceCode;
}
}
接着在model包下建立City类
package com.coolweather.app.model;
/**
* City的实体类,方便对City的操作
* @author aiyuan
* @time 2016.9.7
*
*/
public class City {
private int id;
private String cityName;
private String cityCode;
private int provincdId;
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
public String getCityName(){
return cityName;
}
public void setCityName(String cityName){
this.cityName = cityName;
}
public String getCityCode(){
return cityCode;
}
public void setCityCode(String cityCode){
this.cityCode = cityCode;
}
public int getProvinceId(){
return provincdId;
}
public void setProvinceId(int provinceId){
this.provincdId = provinceId;
}
}
然后在model包下建立County类
package com.coolweather.app.model;
/**
* County的实体类,方便对County的操作
* @author aiyuan
* @time 2016.9.7
*
*/
public class County {
private int id;
private String countyName;
private String countyCode;
private int cityId;
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
public String getCountyName(){
return countyName;
}
public void setCountyName(String countyName){
this.countyName = countyName;
}
public String getCountyCode(){
return countyCode;
}
public void setCountyCode(String countyCode){
this.countyCode = countyCode;
}
public int getCityId(){
return cityId;
}
public void setCityId(int cityId){
this.cityId = cityId;
}
}
是不是感觉很简单,没错,这三个实体类就是只有些字段和一些get,set方法,用于获取和设置相应的字段。
接下来,我们还需要创建一个Cool WeatherDB类,将我们一些常用的数据库操作封装起来,以便后续使用
package com.coolweather.app.db;
import java.util.ArrayList;
import java.util.List;
import com.coolweather.app.model.City;
import com.coolweather.app.model.County;
import com.coolweather.app.model.Province;
import android.Manifest.permission;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
/**
* 把一些常用的数据库操作封装起来,方便日后使用
* @author aiyuan
* @time 2016.9.7
*
*/
public class CoolWeatherDB {
String TAG = "CoolWeatherDB";
/**
* 数据库名
*/
public static final String DB_NAME = "cool_weather";
/**
* 数据库版本
*/
public static final int VERSION = 1;
private static CoolWeatherDB coolWeatherDB;
private SQLiteDatabase db;
/**
* 将构造方法私有化
* @param context
*/
private CoolWeatherDB(Context context) {
CoolWeatherOpenHelper dbHelper = new CoolWeatherOpenHelper(context, DB_NAME, null, VERSION);
Log.i(TAG, "dbHelper create success");
db = dbHelper.getWritableDatabase();
Log.i(TAG, "db create success");
}
/**
* 获取CoolWeatherDB的实例
* @param context
* @return coolWeatherDB
*/
public synchronized static CoolWeatherDB getInstance(Context context){
if(coolWeatherDB == null)
coolWeatherDB = new CoolWeatherDB(context);
return coolWeatherDB;
}
/**
* 将province实例存储到数据库
* @param 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);
}
}
/**
* 从数据库读取全国的所有省份信息
* @return List
*/
public List loadProvince(){
List list = new ArrayList();
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实例存储到数据库
* @param 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);
}
}
/**
* 从数据库读取某省下所有的城市信息
* @param provinceId
* @return List
*/
public List loadCities(int provinceId){
List list = new ArrayList();
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());
}
if(cursor != null)
cursor.close();
return list;
}
/**
* 将County实例 存入数据库
* @param county
*/
public void saveCounty(County county){
if(county != null){
ContentValues values = new ContentValues();
values.put("county_name", county.getCountyName());
values.put("county_code", county.getCountyCode());
values.put("city_id", county.getCityId());
db.insert("County", null, values);
}
}
/**
* 从数据库读取某城市下所有的县信息
* @param cityId
* @return List
*/
public List loadCounties(int cityId){
List list = new ArrayList();
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")));
Log.d(TAG,cursor.getString(cursor.getColumnIndex("county_name"))+"hell");
Log.d(TAG,cursor.getString(cursor.getColumnIndex("county_code"))+"hell");
county.setCityId(cityId);
list.add(county);
}while(cursor.moveToNext());
}
if(cursor != null)
cursor.close();
return list;
}
}
可以看到,这个CoolWeatherDB类并没有继承别的什么类,通过Ctrl+o我们可以看到这个类里面有这么些东西
其中,getInstance方法用于获取Cool WeatherDB的实例,这样就保证在全局范围内只有一个Cool WeatherDB的实例,接下来,有六个方法,
分别用于存储省份数据,读取省份数据,存取城市数据,读取城市数据,存储县数据,读取县数据,下面我以省份的存储和读取为例,讲解一下,城市,县的存储和读取和省的差不多
saveprovince()方法
loadProvince()方法
第一阶段完!!请看下一阶段!
第一行代码酷欧天气开发(二)