您的位置:首页 > 编程语言

第一行代码的最后coolweather的项目实战

2015-10-21 09:12 225 查看
coolweather项目总结

一、创建数据库和表

a.建表语句

create table City (id integer primary key autoincrement,

city_name text,

city_code text,

province_id integer)

注意点:在实体类中将tablename 和 column_?? 变成常量 好处:防止在使用它们的时候出现文字错误,但是要注意在建表语句中注意空格。

b.在db包下创建CoolWeatherOpenHelper类。extends SQLiteOpenHelper

c.为每张对应的表建立实体类。记住将对应的构造方法写完整 有参数无参数最好都写上。

d.创建CoolWeatherDB类,这个类将一些常用的数据库操作封装起来

注意点:使用单例模式,保证整个应用只有一个dbhelper对象

具体操作: private static CoolWeatherDB coolWeaterDB;//静态

/**

* 将构造函数私有化,禁止这个类意外创建dbhelper实体

**/

private CoolWeatherDB (Context context){

CoolWeatherOpenHelper dbHelper = new CoolWeatherOpenHelper(context, DB_NAME, null,

VERSION);

db = dbHelper.getWritableDatabase();

}

/**

*对外提供获取CoolWeatherDB实体的方法

**/

public synchronized static CoolWeatherDB getInstance(Context context){

if(coolWeatherDB == null){

coolWeatherDB = new CoolWeatherDB(context)

}

return coolWeatherDB;

}

数据库的读写操作:

/**

*保存一个实例

**/

public void saveProvince(Province province){

if(province != null){

ContetnValues values = new ContentValues();

values.put(“字段名”,province.getName);

….

db.insert(“表名”,null,values);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: