当前位置: 移动技术网 > IT编程>移动开发>Android > Android 个人理财工具二:使用SQLite实现启动时初始化数据

Android 个人理财工具二:使用SQLite实现启动时初始化数据

2019年07月24日  | 移动技术网IT编程  | 我要评论

coolmosaic,品箫门,睡梦雏龙

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

        系列文章:

                       android 个人理财工具六:显示账单明细 下

                       android 个人理财工具五:显示账单明细 上

                       android 个人理财工具四:添加账单页面 下

                       android 个人理财工具三:添加账单页面 上

                       android 个人理财工具二:使用sqlite实现启动时初始化数据

                       android 个人理财工具一:项目概述与启动界面的实现

       以上就是对android sql的讲解及实例,后续继续做个人理财项目,谢谢大家支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网