Android口袋天气系列四-->数据库封装类

为了方便数据的获取和处理,也为了保证数据库访问的同步性,所以要创建一个数据库封装类



步骤:
          1.创建一个SQLOpenHelperl类,在onCreate()中创建需要用到的表
                    例如:cityQueen,包含城市的基本信息

          2.创建一个数据库封装类 PocketWeatherDB,用来封装数据库的基本操作。
                 使用单例模式创建这个类,因为要保证同步性。

          3.在需要的地方调用
                         pocketWeatherDB=PocketWeatherDB.getInstance(context);



代码如下:
SqlOpenHelper.class
          
          

/**
*  SQLOpenHelper类,用来创建和更新数据库
* Created by Administrator on 2016/5/9.
*/
public class SqlOpenHelper extends SQLiteOpenHelper {
   
private static  final String createCityQeen = "create table if not exists cityQueen(_id integer primary key autoincrement,city_name text,city_lon real,city_lat real,isLocated integer not null,date text)" ;

    public
SqlOpenHelper(Context context , String name) {
       
super (context , name , null, 1 ) ;
   
}

   
@Override
   
public void onCreate (SQLiteDatabase db) {
       
//创建城市列表数据表
       
db.execSQL( createCityQeen ) ;

   
}
   
@Override
   
public void onUpgrade (SQLiteDatabase db , int oldVersion , int newVersion) {

    }
}




PocketWeatherDB.class

/**
* 封装的一些数据库的操作
* 使用单例模式
* Created by Administrator on 2016/5/9.
*/
public class PocketWeatherDB {
   
private SqlOpenHelper sqlOpenHelper ;
    private static
PocketWeatherDB pocketWeatherDB ;
    private
SQLiteDatabase database ;
    public void
setContext (Context context) {
       
this . context = context ;
   
}

   
private Context context ;
    private
PocketWeatherDB(Context context) {
       
sqlOpenHelper = new SqlOpenHelper(context , "pocketWeather.db" ) ;
       
database = sqlOpenHelper .getWritableDatabase() ;
   
}
   
public static PocketWeatherDB getInstance (Context context){
       
if ( pocketWeatherDB == null ){
           
synchronized (PocketWeatherDB. class ){
               
if ( pocketWeatherDB == null ){
                   
pocketWeatherDB = new PocketWeatherDB(context) ;
               
}
            }
        }
       
return pocketWeatherDB ;
   
}
   
/**
     * 存储城市信息
     *  如果是存储的定位城市信息,必须先判断是否有定位城市,如果有,删除,在存储
     *  如果不是定位城市,也要判断是否数据库中已经有了相同的数据,没有,才存储
     */
   
public void saveCityQeen (CityInfo cityInfo){
       
if (cityInfo.getIsLocated()== 1 ){
           
//如果是定位城市
           
Cursor c= database .query( "cityQueen" ,null, "isLocated=?" ,new String[]{ "1" } ,null,null,null ) ;
            if
(c.getCount()!= 0 ){
                ContentValues locCitys=
new ContentValues() ;
               
locCitys.put( "city_name " , cityInfo.getCity_name()) ;
               
locCitys.put( "city_lon" , cityInfo.getCity_lon()) ;
               
locCitys.put( "city_lat" , cityInfo.getCity_lat()) ;
               
locCitys.put( "isLocated" , cityInfo.getIsLocated()) ;
               
locCitys.put( "date" , cityInfo.getDate()) ;
              
database .update( "cityQueen" , locCitys ,null,null ) ;

               
Log.i( "info" , "save1" ) ;
           
}
           
else {
                ContentValues locCitys=
new ContentValues() ;
               
locCitys.put( "city_name " , cityInfo.getCity_name()) ;
               
locCitys.put( "city_lon" , cityInfo.getCity_lon()) ;
               
locCitys.put( "city_lat" , cityInfo.getCity_lat()) ;
               
locCitys.put( "isLocated" , cityInfo.getIsLocated()) ;
               
locCitys.put( "date" , cityInfo.getDate()) ;
               
database .insert( "cityQueen" ,null, locCitys) ;
               
Log.i( "info" , "save2" ) ;
           
}
        }
       
else {
            Cursor c=
database .query( "cityQueen" ,null, "city_name=?" ,new String[]{cityInfo.getCity_name()} ,null,null,null ) ;
            if
(c.getCount()== 0 ){
                ContentValues contentValues=
new ContentValues() ;
               
contentValues.put( "city_name " , cityInfo.getCity_name()) ;
               
contentValues.put( "city_lon" , cityInfo.getCity_lon()) ;
               
contentValues.put( "city_lat" , cityInfo.getCity_lat()) ;
               
contentValues.put( "isLocated" , cityInfo.getIsLocated()) ;
               
contentValues.put( "date" , cityInfo.getDate()) ;
               
database .insert( "cityQueen" ,null, contentValues) ;
           
}

        }
    }
   
/**
     * 查询城市信息
     */
   
public ArrayList<CityInfo> queryCityQueen (){
        ArrayList<CityInfo> list=
new ArrayList<>() ;
       
Cursor c= database .query( "cityQueen" ,null,null,null,null,null,null ) ;
        if
(c!= null ){
            String args[]=c.getColumnNames()
;
            while
(c.moveToNext()){
                CityInfo cityInfo=
new CityInfo() ;
              
cityInfo.setCity_name(c.getString(c.getColumnIndex( "city_name" ))) ;
               
cityInfo.setCity_lon(c.getDouble(c.getColumnIndex( "city_lon" ))) ;
               
cityInfo.setCity_lat(c.getDouble(c.getColumnIndex( "city_lat" ))) ;
               
cityInfo.setIsLocated(c.getInt(c.getColumnIndex( "isLocated" ))) ;
               
cityInfo.setDate(c.getString(c.getColumnIndex( "date" ))) ;
               
list.add(cityInfo) ;
           
}
        }
       
return list ;
   
}
   
/**
     * 更新定位城市名称
     */
   
public void updateCityName (String cityName){
        ContentValues Values=
new ContentValues() ;
       
Values.put( "city_name" , cityName) ;
       
database .update( "cityQueen" , Values , "isLocated=?" ,new String[]{ "1" }) ;
   
}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值