设为首页 加入收藏

TOP

Android数据库编程:SqLiteOpenHelper的使用(一)
2014-11-24 12:37:50 来源: 作者: 【 】 浏览:0
Tags:Android 数据库 编程 SqLiteOpenHelper 使用

1,建库、建表


建立数据库,通常要继承一个类:SqLiteOpenHelper,这个类很实用,通常这个类有三种参数的构造函数。


public class DatabaseHelper extends SQLiteOpenHelper {


public DatabaseHelper(Context context, String name, CursorFactory factory,


int version) {


super(context, name, factory, version);


// TODO Auto-generated constructor stub


}


@Override


public void onCreate(SQLiteDatabase db) {


// TODO Auto-generated method stub



}


@Override


public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


// TODO Auto-generated method stub


}


}


其中的Version是版本号,数据库的版本号的作用是不言而喻的,Version通常可以定义成一个静态类常量,如:private static final int VERSION = 1;


对于构造函数如果觉得不好用,或者说不是太好用,可以自己定义,本文就是自己定义一个构造函数,因为我觉得这样做起来很方便:


public DatabaseHelper(Context context,String name){


this(context,name,VERSION);


}


这样我在实例化DatabaseHelper的时候,只要提供一个context和一个数据库名就OK了。


在onCreate方法里面建立表,如:


@Override


public void onCreate(SQLiteDatabase db) {


// TODO Auto-generated method stub


db.execSQL("create table dish(id integer primary key autoincrement,name nvarchar(10),info text,food_category nvarchar(10),prices double,vip_prices double,category nvarchar(10),picture_path nvarchar(50), " +


" is_best_seller nchar(2),is_recommend nchar(2)) ");


db.execSQL("create table bill(id integer primary key ,dish_name nvarchar(10), " +


" amount integer,prices double,vip_prices double, " +


" total_prices double,total_vip_prices double) ");


System.out.println("create a Database");


}


如果主键是自增长的话,别忘记autoincrement。


注:当我们在实例化DatabaseHelper的时候,数据库并没有创建,而是在调用getReadableDatabase()(只查询),或者是getWritableDatabase()(可增删)的时候才会打开(已经创建了该数据库)或者创建。


2,使用表


很多时候,会看到有些人建了表之后不会使用。在完成一些较大的系统的时候,通常我们会把建库建表放在一个类里面,把增删改查封装到另外的类里面通常叫**DAO,作为该类的服务。


比如我们刚刚在DatabaseHelper里面建立了一个账单表,现在我们要对其查询和删除,在查询的时候把查询的结果(数据集)封装到ArrayList里面,很明显该ArrayList的类型位Bill,另外如果是查询的话,我们需要Cursor合格接口,在更新的时候我们需要ContentValues这个接口,代码如下:


public class BillDao {


private DatabaseHelper dbHelper;


private SQLiteDatabase db;


private Cursor cursor;


private ArrayList list;


public List queryBill(Context context){


try {


list = new ArrayList();


dbHelper = new DatabaseHelper(context,"Emenu_db");


db=dbHelper.getReadableDatabase();


cursor = db.query("bill", new String[]{"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()){


Bill bill = new Bill();


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 (Exception e) {


e.printStackTrace();


// TODO: handle exception


}finally{


try {


if (cursor!=null){


cursor.close();


}


db.close();


} catch

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android 3D物体的移动 下一篇Android中常用适配器及定义自己的..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C++中智能指针的性能 (2025-12-25 03:49:29)
·如何用智能指针实现c (2025-12-25 03:49:27)
·如何在 C 语言中管理 (2025-12-25 03:20:14)
·C语言和内存管理有什 (2025-12-25 03:20:11)
·为什么C语言从不被淘 (2025-12-25 03:20:08)