为了方便数据的获取和处理,也为了保证数据库访问的同步性,所以要创建一个数据库封装类
步骤:
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" }) ;
}
* 封装的一些数据库的操作
* 使用单例模式
* 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" }) ;
}
}