使用嵌入式关系型SQLite数据库存储数据(添删改查)使用(execSQL,rawQuery)

时间:2021-05-20 23:01:12

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

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

 

package cn.itcast.service;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBOpenHelper extends SQLiteOpenHelper {
 private static final String DATABASENAME = "itcast.db"; //数据库名称
 private static final int DATABASEVERSION = 1;//数据库版本

 public DBOpenHelper(Context context) {
  super(context, DATABASENAME, null, DATABASEVERSION);
 }

 @Override
 public void onCreate(SQLiteDatabase db) {//创建的时候就调用,且只调用一次
  db.execSQL("CREATE TABLE person (personid integer primary key autoincrement, name varchar(20), amount integer)");//执行有更改的sql语句
 }

 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {//当软件升级以及版本改变的时候调用
  db.execSQL("DROP TABLE IF EXISTS person");
  onCreate(db);
 }

}

首先通过上面的DBOpenHelper创建数据库,再实现操作数据库对象,代码如下:

package cn.itcast.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import cn.itcast.domain.Person;

public class PersonService {
 private DBOpenHelper dbOpenHelper;
 
 public PersonService(Context context) {
  this.dbOpenHelper = new DBOpenHelper(context);
 }

 public void save(Person person){
  //如果要对数据进行更改,就调用此方法得到用于操作数据库的实例,该方法以读和写方式打开数据库
  SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
  db.execSQL("insert into person (name) values(?)", new Object[]{person.getName()});
 }
 
 public void update(Person person){
  SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
  db.execSQL("update person set name=? where personid=?",
    new Object[]{person.getName(),person.getId()});
 }
 
 public void delete(Integer id){
  SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
  db.execSQL("delete from person where personid=?", new Object[]{id.toString()});
 }
 
 public Person find(Integer id){
  //如果只对数据进行读取,建议使用此方法
  SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
  Cursor cursor = db.rawQuery("select * from person where personid=?", new String[]{id.toString()});
  if(cursor.moveToFirst()){
   int personid = cursor.getInt(cursor.getColumnIndex("personid"));
   String name = cursor.getString(cursor.getColumnIndex("name"));
   return new Person(personid, name);
  }
  return null;
 }
 
 public List<Person> getScrollData(Integer offset, Integer maxResult){
  List<Person> persons = new ArrayList<Person>();
  SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
  Cursor cursor = db.rawQuery("select * from person limit ?,?",
    new String[]{offset.toString(), maxResult.toString()});
  while(cursor.moveToNext()){
   int personid = cursor.getInt(cursor.getColumnIndex("personid"));
   String name = cursor.getString(cursor.getColumnIndex("name"));
   Person person = new Person(personid, name);
   persons.add(person);
  }
  cursor.close();
  return persons;
 }
 
 public long getCount() {
  SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
  Cursor cursor = db.rawQuery("select count(*) from person", null);
  cursor.moveToFirst();
  return cursor.getLong(0);
 }
}