首页 / 操作系统 / Linux / Android数据库编程:SqLiteOpenHelper的使用
Android的数据库编程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}@Overridepublic void onCreate(SQLiteDatabase db) {// TODO Auto-generated method stub }@Overridepublic 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方法里面建立表,如:@Overridepublic void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stubdb.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<Bill> list;public List<Bill> queryBill(Context context){try {list = new ArrayList<Bill>();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 (Exception e2) {e2.printStackTrace();// TODO: handle exception}}return list;}public void DeleteAllBill(Context context){try{ dbHelper = new DatabaseHelper(context,"Emenu_db");db = dbHelper.getWritableDatabase(); db.delete("bill", null, null);cursor.close();cursor.close();}catch(Exception e){e.printStackTrace(); }finally{try {db.close(); } catch (Exception e) { e.printStackTrace();}}}}异常要捕获,最后在使用完的时候要记得关闭cursor和db,cursor要判断先判断是否为空。插表的时候道理是一样,contentValues里芳的是键值对,键是表的字段名,利用db.insert("bill",null ,c)db.update("orders", c, "dish_name=?",new String[]{order.getDishName()}); 注:带有参数的的查询或更新,千万不要忘记占位符‘?’