每个应用程序都要使用数据,Android应用程序也不例外,Android使用开源的、与操作系统无关的SQL数据库 --大名鼎鼎的SQLite。SQLite是一款轻量级数据库,它的设计目的是嵌入式,而且它占用的资源非常少,在嵌入式设备中,可能只需要几百KB,这也是 Android 系统采用 SQLite 数据库的原因之一吧。

简介

  • 轻量级
    使用 SQLite 只需要带一个动态库,就可以享受它的全部功能,而且那个动态库的尺寸想当小。
  • 独立性
    SQLite 数据库的核心引擎不需要依赖第三方软件,也不需要所谓的“安装”。
  • 隔离性
    SQLite 数据库中所有的信息(比如表、视图、触发器等)都包含在一个文件夹内,方便管理和维护。
  • 跨平台
    SQLite 目前支持大部分操作系统,不至电脑操作系统更在众多的手机系统也是能够运行,比如:Android。
  • 多语言接口
    SQLite 数据库支持多语言编程接口。
  • 安全性
    SQLite 数据库通过数据库级上的独占性和共享锁来实现独立事务处理。这意味着多个进程可以在同一时间从同一数据库读取数据,但只能有一个可以写入数据。

SQLite使用介绍
  
首先先来看一下本篇例子继承 SQLiteOpenHelper 类实现的 dbHelper 类。

Using SQLite in Android Application
package com.terry;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class dbHelper extends SQLiteOpenHelper {

    
private final static String DATABASE_NAME="sec_db";
    
private final static int DATABASE_VERSION=1;
    
private final static String TABLE_NAME="sec_pwd";
    
public final static String FIELD_ID="_id"
    
public final static String FIELD_TITLE="sec_Title";
    
    
    
public dbHelper(Context context)
    {
        
super(context, DATABASE_NAME,null, DATABASE_VERSION);
    }
    
    
     
    @Override
    
public void onCreate(SQLiteDatabase db) {
        
// TODO Auto-generated method stub
        String sql="Create table "+TABLE_NAME+"("+FIELD_ID+" integer primary key autoincrement,"
        
+FIELD_TITLE+" text );";
        db.execSQL(sql);
        
         
    }

    @Override
    
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        
// TODO Auto-generated method stub
        String sql=" DROP TABLE IF EXISTS "+TABLE_NAME;
        db.execSQL(sql);
        onCreate(db);
    }

    
public Cursor select()
    {
        SQLiteDatabase db
=this.getReadableDatabase();
        Cursor cursor
=db.query(TABLE_NAME, nullnullnullnullnull,  " _id desc");
        
return cursor;
    }
    
    
public long insert(String Title)
    {
        SQLiteDatabase db
=this.getWritableDatabase();
        ContentValues cv
=new ContentValues(); 
        cv.put(FIELD_TITLE, Title);
        
long row=db.insert(TABLE_NAME, null, cv);
        
return row;
    }
    
    
public void delete(int id)
    {
        SQLiteDatabase db
=this.getWritableDatabase();
        String where
=FIELD_ID+"=?";
        String[] whereValue
={Integer.toString(id)};
        db.delete(TABLE_NAME, where, whereValue);
    }
    
    
public void update(int id,String Title)
    {
        SQLiteDatabase db
=this.getWritableDatabase();
        String where
=FIELD_ID+"=?";
        String[] whereValue
={Integer.toString(id)};
        ContentValues cv
=new ContentValues(); 
        cv.put(FIELD_TITLE, Title);
        db.update(TABLE_NAME, cv, where, whereValue);
    }
    
    
    
    
}
Using SQLite in Android Application

 

  • 创建和打开数据库
    上篇通过构造函数来创建数据库,看一下构造函数的方法
    Using SQLite in Android Application
    android.database.sqlite.SQLiteOpenHelper.SQLiteOpenHelper(Context context, String name, CursorFactory factory, int version)

    public SQLiteOpenHelper (Context context, String name, SQLiteDatabase.CursorFactory factory, int version) 
    Since: API Level 
    1 
    Create a helper object to create, open, and
    /or manage a database. The database is not actually created or opened until one of getWritableDatabase() or getReadableDatabase() is called.

    Parameters
    context  to use to open or create the database 
    name  of the database file, or 
    null for an in-memory database 
    factory  to use 
    for creating cursor objects, or null for the default 
    version  number of the database (starting at 
    1); if the database is older, onUpgrade(SQLiteDatabase, intint) will be used to upgrade the database  

    Public Methods
    Using SQLite in Android Application

     

    大体可以理成如下:如果进入此函数,不存在此数据库则创建,如果存在此数据库则打开连接,只要进入此方法就可以用打开的连接获得getWritableDatabase()或getReadableDatabase()这两个方法。
  • 创建表--》Create Table
    一个数据库中可以包含多个表,每一条数据都存在指定的表中,要创建可以通过 execSQL 方法来执行一条 SQL 语句。上面的方法为
    Using SQLite in Android Application
    代码
    public void onCreate(SQLiteDatabase db) {
            
    // TODO Auto-generated method stub
            String sql="Create table "+TABLE_NAME+"("+FIELD_ID+" integer primary key autoincrement,"
            
    +FIELD_TITLE+" text );";
            db.execSQL(sql);
            
             
        }
    Using SQLite in Android Application

    上面代码创建了表名为“sec_pwd” 的数据表,表内存在一个 integer 类型的主键和一个 text 类型的字段,并执行创建该表。 

  • 添加数据--》Insert
    上面的代码封装了一个使用SQLite 的 insert 方法,向表中添加数据,但是insert 方法要求把数据都打包到 ContentValues 中, ContentValue 其实可就是一个 HashTable,Key值是字段名称,Value 值是字段的值。通过 ContentValues 的put 方法就可以把数据库放到 ContentValue 对象中,然后插入到表中去。代码为:

    Using SQLite in Android Application
    public long insert(String Title)
        {
            SQLiteDatabase db
    =this.getWritableDatabase();
            ContentValues cv
    =new ContentValues(); 
            cv.put(FIELD_TITLE, Title);
            
    long row=db.insert(TABLE_NAME, null, cv);
            
    return row;
        }
    Using SQLite in Android Application

相关文章: