歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android數據庫編程:SqLiteOpenHelper的使用

Android數據庫編程:SqLiteOpenHelper的使用

日期:2017/3/1 11:10:09   编辑:Linux編程

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

}

@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<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()});

注:帶有參數的的查詢或更新,千萬不要忘記占位符‘?’

Copyright © Linux教程網 All Rights Reserved