Android口袋天气系列四-->数据库封装类
2016-05-19 19:03
489 查看
为了方便数据的获取和处理,也为了保证数据库访问的同步性,所以要创建一个数据库封装类
步骤:
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.创建一个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"
});
}
}
相关文章推荐
- Android项目重构之路:实现篇
- Android口袋天气系列三-->天气数据的获取
- Android技术积累:开发规范
- 高德地图组件在Android的应用以及Android与JavaScript的交互(二)
- Android口袋天气系列二-->百度定位
- Android 4.4 以上实现透明导航栏和状态栏 Translucent system bar
- 修改Android系统字号(二)
- 从头学android_创建xml文件
- Android Camera 实时滤镜
- android dexposed框架hook使用实例
- Android Context原理与使用的总结
- Android平台免Root无侵入AOP框架Dexposed使用详解
- 基于AOA协议实现Android设备的USB通信
- android中xmlns:tools属性详解
- Android OpenMobilApi (银联支付 开发环境搭建)
- Android ANR 分析解决方法
- Android万能下拉刷新控件SwipeRefleshLayout使用
- 通过源码分析Android 的消息处理机制
- Android各控件Demo下载地址
- Android Drawable Resources系列6:<transition>