使用嵌入式关系型SQLite数据库存储数据

来源:互联网 发布:电信网络测速在线 编辑:程序博客网 时间:2024/05/16 01:31

除了可以使用文件或SharedPreferences存储数据,还可以选择使用SQLite数据库存储数据。
在Android平台上,集成了一个嵌入式关系型数据库—SQLite,SQLite3支持 NULL、INTEGER、REAL(浮点数字)、TEXT(字符串文本)和BLOB(二进制对象)数据类型,虽然它支持的类型只有五种,但实际上sqlite3也接受varchar(n)、char(n)、decimal(p,s) 等数据类型,只不过在运算或保存时会转成对应的五种数据类型。 SQLite最大的特点是你可以保存任何类型的数据到任何字段中,无论这列声明的数据类型是什么。例如:可以在Integer类型的字段中存放字符串,或者在布尔型字段中存放浮点数,或者在字符型字段中存放日期型值。 但有一种情况例外:定义为INTEGER PRIMARY KEY的字段只能存储64位整数, 当向这种字段中保存除整数以外的数据时,将会产生错误。 另外, SQLite 在解析CREATE TABLE 语句时,会忽略 CREATE TABLE 语句中跟在字段名后面的数据类型信息,如下面语句会忽略 name字段的类型信息:
CREATE TABLE person (personid integer primary key autoincrement, name varchar(20))

SQLite可以解析大部分标准SQL语句,如:
查询语句:select * from 表名 where 条件子句 group by 分组字句 having ... order by 排序子句
如:select * from person
        select * from person order by id desc
        select name from person group by name having count(*)>1
分页SQL与mysql类似,下面SQL语句获取5条记录,跳过前面3条记录
select * from Account limit 5 offset 3 或者 select * from Account limit 3,5
插入语句:insert into 表名(字段列表) values(值列表)。如: insert into person(name, age) values(‘传智’,3)
更新语句:update 表名 set 字段名=值 where 条件子句。如:update person set name=‘传智‘ where id=10
删除语句:delete from 表名 where 条件子句。如:delete from person  where id=10

一、使用SQLiteOpenHelper对数据库进行版本管理

我们在编写数据库应用软件时,需要考虑这样的问题:因为我们开发的软件可能会安装在成百上千个用户的手机上,如果应用使用到了SQLite数据库,我们必须在用户初次使用软件时创建出应用使用到的数据库表结构及添加一些初始化记录,另外在软件升级的时候,也需要对数据表结构进行更新。那么,我们如何才能实现在用户初次使用或升级软件时自动在用户的手机上创建出应用需要的数据库表呢?总不能让我们在每个需要安装此软件的手机上通过手工方式创建数据库表吧?因为这种需求是每个数据库应用都要面临的,所以在Android系统,为我们提供了一个名为SQLiteOpenHelper的抽象类,必须继承它才能使用,它是通过对数据库版本进行管理来实现前面提出的需求。

为了实现对数据库版本进行管理,SQLiteOpenHelper类提供了两个重要的方法,分别是onCreate(SQLiteDatabase db)和onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion),前者用于初次使用软件时生成数据库表,后者用于升级软件时更新数据库表结构。当调用SQLiteOpenHelper的getWritableDatabase()或者getReadableDatabase()方法获取用于操作数据库的SQLiteDatabase实例的时候,如果数据库不存在,Android系统会自动生成一个数据库,接着调用onCreate()方法,onCreate()方法在初次生成数据库时才会被调用,在onCreate()方法里可以生成数据库表结构及添加一些应用使用到的初始化数据。onUpgrade()方法在数据库的版本发生变化时会被调用,一般在软件升级时才需改变版本号,而数据库的版本是由程序员控制的,假设数据库现在的版本是1,由于业务的变更,修改了数据库表结构,这时候就需要升级软件,升级软件时希望更新用户手机里的数据库表结构,为了实现这一目的,可以把原来的数据库版本设置为2(有同学问设置为3行不行?当然可以,如果你愿意,设置为100也行),并且在onUpgrade()方法里面实现表结构的更新。当软件的版本升级次数比较多,这时在onUpgrade()方法里面可以根据原版号和目标版本号进行判断,然后作出相应的表结构及数据更新。

getWritableDatabase()和getReadableDatabase()方法都可以获取一个用于操作数据库的SQLiteDatabase实例。但getWritableDatabase() 方法以读写方式打开数据库,一旦数据库的磁盘空间满了,数据库就只能读而不能写,倘若使用的是getWritableDatabase() 方法就会出错。getReadableDatabase()方法先以读写方式打开数据库,如果数据库的磁盘空间满了,就会打开失败,当打开失败后会继续尝试以只读方式打开数据库。

public class DatabaseHelper extends SQLiteOpenHelper {
    //类没有实例化,是不能用作父类构造器的参数,必须声明为静态
         private static final String name = "itcast"; //数据库名称
         private static final int version = 1; //数据库版本
         public DatabaseHelper(Context context) {
//第三个参数CursorFactory指定在执行查询时获得一个游标实例的工厂类,设置为null,代表使用系统默认的工厂类
                super(context, name, null, version);
         }
        @Override public void onCreate(SQLiteDatabase db) {
              db.execSQL("CREATE TABLE IF NOT EXISTS person (personid integer primary key autoincrement, name varchar(20), age INTEGER)");  
         }
        @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
               db.execSQL("DROP TABLE IF EXISTS person");
               onCreate(db);
         }
}
上面onUpgrade()方法在数据库版本每次发生变化时都会把用户手机上的数据库表删除,然后再重新创建。一般在实际项目中是不能这样做的,正确的做法是在更新数据库表结构时,还要考虑用户存放于数据库中的数据不会丢失。

代码示例:

1、写一个DBSQLLiteOpenHelper继承SQLiteOpenHelper

package com.example.lession05_db.db;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;import android.util.Log;public class DBSQLLiteOpenHelper extends SQLiteOpenHelper {private static final String  name="CSDN.db";private static final int version=3;public DBSQLLiteOpenHelper(Context context) {super(context, name, null, version);Log.v("DBSQLLiteOpenHelper","构造器--------");}@Overridepublic void onCreate(SQLiteDatabase db) {//execSQL来执行sql语句db.execSQL("create table person(personid integer primary key autoincrement,name varchar(20),age integer)");Log.v("DBSQLLiteOpenHelper", "创建执行一次");}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {db.execSQL("alter table person add account integer");Log.v("DBSQLLiteOpenHelper", "当数据库版本更新的时候执行-----每次更新都执行一次");}}


 

2、根据创建的表写一个实体bean

package com.example.lession05_db.domain;public class Person {private Integer id;private String name;private Integer age;private Integer account;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Integer getAccount() {return account;}public void setAccount(Integer account) {this.account = account;}@Overridepublic String toString() {return "Person [id=" + id + ", name=" + name + ", age=" + age+ ", account=" + account + "]";}public Person(Integer id, String name, Integer age, Integer account) {super();this.id = id;this.name = name;this.age = age;this.account = account;}public Person() {super();}}


3、封转一个接口,里面有增删改查的方法体

package com.example.lession05_db.dao;import java.util.List;import android.database.sqlite.SQLiteDatabase;import com.example.lession05_db.domain.Person;public interface PersonDao {//增加的操作public void insert(SQLiteDatabase db,Person entity);//修改的操作public void update(SQLiteDatabase db,Person entity);//删除的操作public void delete(SQLiteDatabase db,Integer id);//查询所有public List<Person> findAll(SQLiteDatabase db);//分页查询public List<Person> getNowPageInfo(SQLiteDatabase db,int nowpage,int pagesize);//按条件查询public Person findById(SQLiteDatabase db,Integer id);}

4、写一个实现类实现那个接口

package com.example.lession05_db.dao;import java.util.ArrayList;import java.util.List;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import com.example.lession05_db.domain.Person;public class PersonDaoImpl implements PersonDao {@Overridepublic void insert(SQLiteDatabase db, Person entity) {if (db.isOpen()) {db.execSQL("insert into person(name,age,account) values(?,?,?)",new Object[] { entity.getName(), entity.getAge(),entity.getAccount() });db.close();}}@Overridepublic void update(SQLiteDatabase db, Person entity) {if (db.isOpen()) {db.execSQL("update person set name=?,age=?,account=? where personid=?",new Object[] { entity.getName(), entity.getAge(),entity.getAccount(),entity.getId() });db.close();}}@Overridepublic void delete(SQLiteDatabase db, Integer id) {if (db.isOpen()) {db.execSQL("delete from person where personid=?", new Object[]{id});db.close();}}@Overridepublic List<Person> findAll(SQLiteDatabase db) {List<Person> persons=new ArrayList<Person>();if(db.isOpen()){Cursor cursor=db.rawQuery("select personid,name,age,account from person", null);while(cursor.moveToNext()){Person person=new Person();person.setId(cursor.getInt(0));person.setName(cursor.getString(1));person.setAge(cursor.getInt(2));person.setAccount(cursor.getInt(3));persons.add(person);}}return persons;}@Overridepublic List<Person> getNowPageInfo(SQLiteDatabase db, int nowpage,int pagesize) {int start=(nowpage-1)*pagesize;List<Person> persons=new ArrayList<Person>();if(db.isOpen()){Cursor cursor=db.rawQuery("select personid,name,age,account from person limit ?,?",new String[]{start+"",pagesize+""} );while(cursor.moveToNext()){Person person=new Person();person.setId(cursor.getInt(0));person.setName(cursor.getString(1));person.setAge(cursor.getInt(2));person.setAccount(cursor.getInt(3));persons.add(person);}}return persons;}@Overridepublic Person findById(SQLiteDatabase db, Integer id) {Person person=null;if(db.isOpen()){Cursor cursor=db.rawQuery("select personid,name,age,account from person where personid=?", new String[]{id+""});while(cursor.moveToNext()){person=new Person();person.setId(cursor.getInt(0));person.setName(cursor.getString(1));person.setAge(cursor.getInt(2));person.setAccount(cursor.getInt(3));}}return person;}}


5、进行测试

首先在项目清单中添加User Library

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.lession05_db"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.example.lession05_db"></instrumentation>    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.lession05_db.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <uses-library android:name="android.test.runner"/>    </application></manifest>


其次写一个测试类进行测试

package com.example.lession05_db.test;import java.util.List;import com.example.lession05_db.dao.PersonDao;import com.example.lession05_db.dao.PersonDaoImpl;import com.example.lession05_db.db.DBSQLLiteOpenHelper;import com.example.lession05_db.domain.Person;import android.database.sqlite.SQLiteDatabase;import android.test.AndroidTestCase;public class DBTest extends AndroidTestCase{PersonDao personDao=new PersonDaoImpl();public void createDB(){DBSQLLiteOpenHelper db=new DBSQLLiteOpenHelper(this.getContext());SQLiteDatabase sdb=db.getWritableDatabase();}public void insert(){DBSQLLiteOpenHelper db=new DBSQLLiteOpenHelper(this.getContext());for(int i=1;i<10;i++){SQLiteDatabase sdb=db.getWritableDatabase();Person entity=new Person(null, "chenhj"+i, 29, 10000+i);personDao.insert(sdb, entity);}}public void update(){DBSQLLiteOpenHelper db=new DBSQLLiteOpenHelper(this.getContext());SQLiteDatabase sdb=db.getWritableDatabase();Person entity=new Person(1, "shuangshuang", 22, 100009);personDao.update(sdb, entity);}public void delete(){DBSQLLiteOpenHelper db=new DBSQLLiteOpenHelper(this.getContext());SQLiteDatabase sdb=db.getWritableDatabase();personDao.delete(sdb, 1);}public void findAll(){DBSQLLiteOpenHelper db=new DBSQLLiteOpenHelper(this.getContext());SQLiteDatabase sdb=db.getWritableDatabase();List<Person> persons=personDao.findAll(sdb);for(Person p:persons){System.out.println(p.toString());}}public void findById(){DBSQLLiteOpenHelper db=new DBSQLLiteOpenHelper(this.getContext());SQLiteDatabase sdb=db.getReadableDatabase();Person p=personDao.findById(sdb, 2);System.out.println(p.toString());}public void page(){DBSQLLiteOpenHelper db=new DBSQLLiteOpenHelper(this.getContext());SQLiteDatabase sdb=db.getReadableDatabase();List<Person> persons=personDao.getNowPageInfo(sdb, 1, 3);for(Person p:persons){System.out.println(p.toString());}}}


 

原创粉丝点击