第一行代码的最后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);
}
}
一、创建数据库和表
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);
}
}
相关文章推荐
- 利用java提取Excel中日平均温度计算及导出
- 优秀而又实用的PHP工具集锦
- java反射深入理解
- C# 之 Word光标移动 GoTo 方法
- Spring MVC提供的表单标签
- 浅谈Java编程中的内存泄露情况
- thinkphp四种url访问方式详解
- wpf 中AxShockwaveFlash重写以及屏蔽鼠标右键
- Requests+Beautifulsoup+re百度贴吧抓取实例(python3.4逐行解释)
- 怎么解决python "Non-ASCII character"错误
- php淘宝评论采集
- Java IO和Java NIO在文件拷贝上的性能差异分析
- C++ Boost 好资源
- c++ iterator
- SpringMVC基于注解映射
- Mac下Java JDK的下载安装和配置
- SpringMVC访问资源文件
- java ==,hashCode(),equals(),
- SpringMVC前端控制器-----DispatcherServlet
- (三)黑马程序员——c语言之字符串处理函数