Android Personal Finance Tools two: Use SQLite to implement initialization data at startup _android

Source: Internet
Author: User
Tags sqlite create database

About SQLite

SQLite is an extension of the embedded SQL Database engine SQLite (SQLite embeddable SQL DB Engine). SQLite is an implementation of the embedded SQL database engine small C language library (c library), implemented a stand-alone, embeddable, 0-configured SQL database engine. Features include: Transaction operations are atomic, consistent, isolated, and persistent, even after system crashes and power failures. 0 configuration-No installation and management is required. The majority of SQL92 standards have been achieved.

I was concerned about the development of sqlite years ago, very optimistic about the prospects of SQLite, because in the mobile, embedded applications, SQLite has a very good feature to meet demand.

As early as Symbian 9.0, Openc out, I studied SQLite to Symbian transplant. Later, symbian9.3 Nokia has integrated SQLite.

So far j2me still do not support SQLite, can be said to be a regret.

Now let's look at the key APIs in the Android Sqlitedatabase package:

Java code

Static Sqlitedatabase openorcreatedatabase (String path, sqlitedatabase.cursorfactory Factory)//Open database 
Cursor Query (string table, string[] columns, string selection, string[] Selectionargs, String groupBy, String having, string Orde Rby, string limit)//execute query SQL 
void Execsql (String sql)//execute non-query SQL 

The SDK 1.0 related APIs for cursor and SQLite have changed a lot for the previous version.

I think the key is to not have query (String SQL) This simple method, very uncomfortable.

But if you learn more about the new query method, you'll find that it's the same.

Instance Code

Let's take a look at two examples.

Java code

Executes the 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 (); 
Execute a multiple table query 
//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+ ""); 
} 

Now let's see how we can apply it in this financial tool.

We need to create the database at the first start of the program, then create the base table and initialize the accounts table.

For the Initapp method in the previous article, we need to transform it into:

Java code

public void Initapp () { 
 billdbhelper billdb=new billdbhelper (this); 
 Billdb. Firststart (); 
 Billdb.close (); 
} 

Below we give the Billdbhelper.java code:

Java code

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 is 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); 
 Create Database LOG.V (TAG, "db path=" +db.getpath ()); public void Createtable_acctitem () {try{db.execsql ("CREATE TABLE Acctitem ("///Created Account table + "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.");} public void creatEtable_bills () {try{db.execsql ("CREATE TABLE Bills" + "ID integer PRIMARY KEY," + "fee Integer," + "Useri 
 D 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.");}  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.");} 
 public void Initacctitem () {db.execsql ("INSERT into acctitem values (100,0, ' revenue ', 0)"); 
 Db.execsql ("INSERT into acctitem values (100100,100, ' wages ', 0)"); 
 Db.execsql ("INSERT into acctitem values (200,0, ' expenditure ', 1)"); 
 Db.execsql ("INSERT into acctitem values (200100,200, ' Daily Necessities ', 1)"); 
 Db.execsql ("INSERT into acctitem values (200101,200, ' utility gas fee ', 1)"); Db.execsql ("Insert INto acctitem values (200103,200, ' petrol fee ', 1) "); 
 
 LOG.V ("Cola", "insert into OK"); 
 The public void Querytable_acctitem () {} is public void Firststart () {//If the first time it is started, there is no colaconfig this table. 
 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 (); 
 } 
}

Series of articles:

Android Personal Finance Tool Six: Show billing details below

Android Personal Finance tool five: show billing details

Android Personal Finance Tools Four: Add Bill page below

Android Personal Finance Tool Three: Add Bill page

Android Personal Finance Tools two: Use SQLite to implement initialization data at startup

Android Personal Finance Tools One: Project overview and implementation of the launch interface

The above is on the Android SQL explanation and example, the follow-up to continue to do a personal finance project, thank you for your support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.