本節所講內容為使用SQLite實現在啟動時初始化數據。
關於SQLite
sqlite是嵌入式SQL數據庫引擎SQLite(SQLite Embeddable SQL Database Engine)的一個擴展。SQLite是一個實現嵌入式SQL數據庫引擎小型C語言庫(C library),實現了獨立的,可嵌入的,零配置的SQL數據庫引擎。特性包括:事務操作是原子,一致,孤立,並且持久的,即使在系統崩潰和電源故障之後。 零配置——不需要安裝和管理。 實現了絕大多數SQL92標准。
我在多年前就關注sqlite的發展,非常看好sqlite的前景,因為在移動、嵌入式的應用裡面,sqlite具有非常好的特性來滿足需求。
早在symbian 9.0 之前,openc 出來後,我就研究sqlite到symbian的移植。後來symbian9.3 nokia就已經集成了sqlite。
至今j2me還不支持sqlite,可以說是個遺憾。
現在我們來看看android sqlitedatabase 包裡面的關鍵api:
Java代碼
- static SQLiteDatabase openOrCreateDatabase(String path, SQLiteDatabase.CursorFactory factory) //打開數據庫
- Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) //執行查詢SQL
- void execSQL(String sql) //執行非查詢sql
sdk 1.0 關於cursor和sqlite的相關api對於前面的版本改變很多。
我覺得關鍵是沒了query(String sql)這個簡單的方法了,很不爽。
不過如果你對新的query方法了解深入點,發現其實也就一樣。
實例代碼
我們來看兩個例子。
Java代碼
- //執行select type,name from sqlite_master where name='colaconfig'
- String col[] = {"type", "name" };
- Cursor c =db.query("sqlite_master", col, "name='colaconfig'", null, null, null, null);
- int n=c.getCount();
- //執行多表查詢
- //select fee,desc from acctite a,bills b where a.id=b.id
- String col2[] = {"fee", "desc" };
- Cursor c2 =db.query("acctitem a,bills b", col, "a.id=b.id", null, null, null, null);
- int n2=c2.getCount();
- Log.v("cola","c2.getCount="+n2+"");
-
- c2.moveToFirst();
- int k = 0;
- while(!c2.isAfterLast()){
- String ss = c2.getString(0) +", "+ c2.getString(1);
- c2.moveToNext();
-
- Log.v("cola","ss="+ss+"");
- }
現在來看看我們如何在這個理財工具裡面應用它。
我們需要在程序的第一次啟動時,創建數據庫,然後把基本的表創建好,並且初始化好賬目表。
對於上一篇中的initapp方法,我們需要改造成:
Java代碼
- public void initApp(){
- BilldbHelper billdb=new BilldbHelper(this);
- billdb.FirstStart();
- billdb.close();
- }
下面我們給出BilldbHelper.java 代碼:
Java代碼
- package com.cola.ui;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.util.Log;
- /**
- * Provides access to a database of notes. Each note has a title, the note
- * itself, a creation date and a modified data.
- */
- public class BilldbHelper {
- private static final String TAG = "Cola_BilldbHelper";
- private static final String DATABASE_NAME = "cola.db";
-
- SQLiteDatabase db;
- Context context;
-
- BilldbHelper(Context _context) {
- context=_context;
- db=context.openOrCreateDatabase(DATABASE_NAME, 0, null); //創建數據庫
- Log.v(TAG,"db path="+db.getPath());
- }
-
- public void CreateTable_acctitem() {
- try{
- db.execSQL("CREATE TABLE acctitem (" //創建賬目表
- + "ID INTEGER PRIMARY KEY,"
- + "PID integer,"
- + "NAME TEXT,"
- + "TYPE INTEGER"
- + ");");
- Log.v("cola","Create Table acctitem ok");
- }catch(Exception e){
- Log.v("cola","Create Table acctitem err,table exists.");
- }
- }
-
- public void CreateTable_bills() {
- try{
- db.execSQL("CREATE TABLE bills ("
- + "ID INTEGER PRIMARY KEY,"
- + "fee integer,"
- + "userid integer,"
- + "sdate TEXT,"
- + "stime TEXT,"
- + "desc TEXT"
- + ");");
- Log.v("cola","Create Table acctitem ok");
- }catch(Exception e){
- Log.v("cola","Create Table acctitem err,table exists.");
- }
- }
-
- public void CreateTable_colaconfig() {
- try{
- db.execSQL("CREATE TABLE colaconfig ("
- + "ID INTEGER PRIMARY KEY,"
- + "NAME TEXT"
- + ");");
- Log.v("cola","Create Table colaconfig ok");
- }catch(Exception e){
- Log.v("cola","Create Table acctitem err,table exists.");
- }
- }
-
- public void InitAcctitem() {
-
- db.execSQL("insert into acctitem values (100,0,'收入',0)");
- db.execSQL("insert into acctitem values (100100,100,'工資',0)");
- db.execSQL("insert into acctitem values (200,0,'支出',1)");
- db.execSQL("insert into acctitem values (200100,200,'生活用品',1)");
- db.execSQL("insert into acctitem values (200101,200,'水電煤氣費',1)");
- db.execSQL("insert into acctitem values (200103,200,'汽油費',1)");
- Log.v("cola","insert into ok");
-
- }
-
-
- public void QueryTable_acctitem(){
-
- }
-
- public void FirstStart(){
- //如果是第一次啟動,就不存在colaconfig這張表.
- try{
- String col[] = {"type", "name" };
- Cursor c =db.query("sqlite_master", col, "name='colaconfig'", null, null, null, null);
- int n=c.getCount();
- if (c.getCount()==0){
- CreateTable_acctitem();
- CreateTable_colaconfig();
- CreateTable_bills();
- InitAcctitem();
-
- }
-
- Log.v("cola","c.getCount="+n+"");
-
-
-
- }catch(Exception e){
- Log.v("cola","e="+e.getMessage());
- }
-
- }
-
- public void close(){
- db.close();
- }
- }