实例二

来源:互联网 发布:matlab求矩阵特征向量 编辑:程序博客网 时间:2024/04/20 02:19

关于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

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方法了解深入点,发现其实也就一样.

我们来看2个例子

//执行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方法 ,我们需要改造成

    public void initApp(){
         BilldbHelper billdb=new BilldbHelper(this);
         billdb.FirstStart();
         billdb.close();
         
    }
下面我们给出BilldbHelper.java 代码

原创粉丝点击