android数据库编程----SqLiteOpenHelper的使用
Android的数据库编程
1,建库、建表
建立数据库,通常要继承一个类:SqLiteOpenHelper,这个类很实用,通常这个类有三种参数的构造函数。
publicclassDatabaseHelperextendsSQLiteOpenHelper{
publicDatabaseHelper(Contextcontext,Stringname,CursorFactoryfactory,
intversion){
super(context,name,factory,version);
//TODOAuto-generatedconstructorstub
}
@Override
publicvoidonCreate(SQLiteDatabasedb){
//TODOAuto-generatedmethodstub
}
@Override
publicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion){
//TODOAuto-generatedmethodstub
}
}
其中的Version是版本号,数据库的版本号的作用是不言而喻的,Version通常可以定义成一个静态类常量,如:privatestaticfinalintVERSION=1;
对于构造函数如果觉得不好用,或者说不是太好用,可以自己定义,本文就是自己定义一个构造函数,因为我觉得这样做起来很方便:
publicDatabaseHelper(Contextcontext,Stringname){
this(context,name,VERSION);
}
这样我在实例化DatabaseHelper的时候,只要提供一个context和一个数据库名就OK了。
在onCreate方法里面建立表,如:
@Override
publicvoidonCreate(SQLiteDatabasedb){
//TODOAuto-generatedmethodstub
db.execSQL("createtabledish(idintegerprimarykeyautoincrement,namenvarchar(10),infotext,food_categorynvarchar(10),pricesdouble,vip_pricesdouble,categorynvarchar(10),picture_pathnvarchar(50),"+
"is_best_sellernchar(2),is_recommendnchar(2))");
db.execSQL("createtablebill(idintegerprimarykey,dish_namenvarchar(10),"+
"amountinteger,pricesdouble,vip_pricesdouble,"+
"total_pricesdouble,total_vip_pricesdouble)");
System.out.println("createaDatabase");
}
如果主键是自增长的话,别忘记autoincrement。
注:当我们在实例化DatabaseHelper的时候,数据库并没有创建,而是在调用getReadableDatabase()(只查询),或者是getWritableDatabase()(可增删)的时候才会打开(已经创建了该数据库)或者创建。
2,使用表
很多时候,会看到有些人建了表之后不会使用。在完成一些较大的系统的时候,通常我们会把建库建表放在一个类里面,把增删改查封装到另外的类里面通常叫**DAO,作为该类的服务。
比如我们刚刚在DatabaseHelper里面建立了一个账单表,现在我们要对其查询和删除,在查询的时候把查询的结果(数据集)封装到ArrayList里面,很明显该ArrayList的类型位Bill,另外如果是查询的话,我们需要Cursor合格接口,在更新的时候我们需要ContentValues这个接口,代码如下:
publicclassBillDao{
privateDatabaseHelperdbHelper;
privateSQLiteDatabasedb;
privateCursorcursor;
privateArrayList<Bill>list;
publicList<Bill>queryBill(Contextcontext){
try{
list=newArrayList<Bill>();
dbHelper=newDatabaseHelper(context,"Emenu_db");
db=dbHelper.getReadableDatabase();
cursor=db.query("bill",newString[]{"id","dish_name","amount","prices",
"vip_prices","total_prices","total_vip_prices"},null,null,null,null,null);
// if(cursor!=null&&cursor.moveToFirst()){
System.out.println(cursor.getCount());
while(cursor.moveToNext()){
Billbill=newBill();
bill.setId(cursor.getInt(cursor.getColumnIndex("id")));
bill.setDishName(cursor.getString(cursor.getColumnIndex("dish_name")));
bill.setDishAmount(cursor.getInt(cursor.getColumnIndex("amount")));
bill.setPrices(cursor.getDouble((cursor.getColumnIndex("prices"))));
bill.setVipPrices(cursor.getDouble(cursor.getColumnIndex("vip_prices")));
// order.setDishVipTotalPrice(cursor.getDouble(cursor.getColumnIndex("dishes_vip_prices")));
// order.setDishes_prices(cursor.getDouble(cursor.getColumnIndex("dishes_prices")));
bill.setTotalPrices(cursor.getDouble(cursor.getColumnIndex("total_prices")));
bill.setTotalVipPrices(cursor.getDouble(cursor.getColumnIndex("total_vip_prices")));
list.add(bill);
}
// }
}catch(Exceptione){
e.printStackTrace();
//TODO:handleexception
}finally{
try{
if(cursor!=null){
cursor.close();
}
db.close();
}catch(Exceptione2){
e2.printStackTrace();
//TODO:handleexception
}
}
returnlist;
}
publicvoidDeleteAllBill(Contextcontext){
try{
dbHelper=newDatabaseHelper(context,"Emenu_db");
db=dbHelper.getWritableDatabase();
db.delete("bill",null,null);
cursor.close();
cursor.close();
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
db.close();
}catch(Exceptione){
e.printStackTrace();
}
}
}
}
异常要捕获,最后在使用完的时候要记得关闭cursor和db,cursor要判断先判断是否为空。
插表的时候道理是一样,
contentValues里芳的是键值对,键是表的字段名,利用db.insert("bill",null,c)
db.update("orders", c, "dish_name=?",new String[]{order.getDishName()});
注:带有参数的的查询或更新,千万不要忘记占位符‘?’
更多相关文章
- Android: 自定义Tab样式
- Android 自定义View及其在布局文件中的使用示例(二)
- Android快速开发框架Android_BaseLib,集成了常用工具类,自定义View
- Android OpenGL库函数列表
- Android中自定义权限
- Android短彩信数据库信息整理
- android复制数据库到SD卡
- Android 数据库 大量插入 事务开启
- Android 自定义像素AVD模拟器无键盘