Android SQLite数据库增删改查操作的案例分析
person实体类
package com.ljq.domain;
public class person {
private integer id;
private string name;
private string phone;
public person() {
super();
}
public person(string name, string phone) {
super();
this.name = name;
this.phone = phone;
}
public person(integer id, string name, string phone) {
super();
this.id = id;
this.name = name;
this.phone = phone;
}
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 string getphone() {
return phone;
}
public void setphone(string phone) {
this.phone = phone;
}
}
dbopenhelper数据库关联类
package com.ljq.db;
import android.content.context;
import android.database.sqlite.sqlitedatabase;
import android.database.sqlite.sqliteopenhelper;
public class dbopenhelper extends sqliteopenhelper {
// 类没有实例化,是不能用作父类构造器的参数,必须声明为静态
private static final string dbname = "ljq.db";
private static final int version = 1;
// 第三个参数cursorfactory指定在执行查询时获得一个游标实例的工厂类,
// 设置为null,代表使用系统默认的工厂类
public dbopenhelper(context context) {
super(context, dbname, null, version);
}
@override
public void oncreate(sqlitedatabase db) {
db.execsql("create table person (id integer primary key autoincrement, name varchar(20), phone varchar(20))");
}
@override
public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {
// 注:生产环境上不能做删除操作
db.execsql("drop table if exists person");
oncreate(db);
}
}
personservice业务类
package com.ljq.db;
import java.util.arraylist;
import java.util.list;
import android.content.context;
import android.database.cursor;
import com.ljq.domain.person;
public class personservice {
private dbopenhelper dbopenhelper = null;
/**
* 构造函数
*
* 调用getwritabledatabase()或getreadabledatabase()方法后,会缓存sqlitedatabase实例;
* 因为这里是手机应用程序,一般只有一个用户访问数据库,所以建议不关闭数据库,保持连接状态。
* getwritabledatabase(),getreadabledatabase的区别是当数据库写满时,调用前者会报错,调用后者不会,
* 所以如果不是更新数据库的话,最好调用后者来获得数据库连接。
*
* 对于熟悉sql语句的程序员最好使用exesql(),rawquery(),因为比较直观明了
*
* @param context
*/
public personservice(context context){
dbopenhelper = new dbopenhelper(context);
}
public void save(person person){
dbopenhelper.getwritabledatabase().execsql("insert into person(name, phone) values (?, ?)",
new object[]{person.getname(), person.getphone()});
}
public void update(person person){
dbopenhelper.getwritabledatabase().execsql("update person set name=?, phone=? where id=?",
new object[]{person.getname(), person.getphone(), person.getid()});
}
public void delete(integer... ids){
if(ids.length>0){
stringbuffer sb = new stringbuffer();
for(integer id : ids){
sb.append("?").append(",");
}
sb.deletecharat(sb.length() - 1);
dbopenhelper.getwritabledatabase().execsql("delete from person where id in ("+sb+")", (object[])ids);
}
}
public person find(integer id){
cursor cursor = dbopenhelper.getreadabledatabase().rawquery("select id, name, phone from person where id=?",
new string[]{string.valueof(id)});
if(cursor.movetonext()){
int personid = cursor.getint(0);
string name = cursor.getstring(1);
string phone = cursor.getstring(2);
return new person(personid, name, phone);
}
return null;
}
public long getcount(){
cursor cursor = dbopenhelper.getreadabledatabase().query("person",
new string[]{"count(*)"}, null,null,null,null,null);
if(cursor.movetonext()){
return cursor.getlong(0);
}
return 0;
}
/**
* 分页
*
* @param startresult 偏移量,默认从0开始
* @param maxresult 每页显示的条数
* @return
*/
public list<person> getscrolldata(int startresult, int maxresult){
list<person> persons = new arraylist<person>();
//cursor cursor = dbopenhelper.getreadabledatabase().query("person", new string[]{"id, name, phone"},
// "name like ?", new string[]{"%ljq%"}, null, null, "id desc", "1,2");
cursor cursor = dbopenhelper.getreadabledatabase().rawquery("select * from person limit ?,?",
new string[]{string.valueof(startresult), string.valueof(maxresult)});
while(cursor.movetonext()) {
int personid = cursor.getint(0);
string name = cursor.getstring(1);
string phone = cursor.getstring(2);
persons.add(new person(personid, name, phone));
}
return persons;
}
}
personservicetest测试类
package com.ljq.test;
import java.util.list;
import com.ljq.db.personservice;
import com.ljq.domain.person;
import android.test.androidtestcase;
import android.util.log;
public class personservicetest extends androidtestcase{
private final string tag = "personservicetest";
public void testsave() throws exception{
personservice personservice = new personservice(this.getcontext());
personservice.save(new person("zhangsan1", "059188893343"));
personservice.save(new person("zhangsan2", "059188893343"));
personservice.save(new person("zhangsan3", "059188893343"));
personservice.save(new person("zhangsan4", "059188893343"));
personservice.save(new person("zhangsan5", "059188893343"));
}
public void testupdate() throws exception{
personservice personservice = new personservice(this.getcontext());
person person = personservice.find(1);
person.setname("linjiqin");
personservice.update(person);
}
public void testfind() throws exception{
personservice personservice = new personservice(this.getcontext());
person person = personservice.find(1);
log.i(tag, person.getname());
}
public void testlist() throws exception{
personservice personservice = new personservice(this.getcontext());
list<person> persons = personservice.getscrolldata(0, 10);
for(person person : persons){
log.i(tag, person.getid() + " : " + person.getname());
}
}
public void testcount() throws exception{
personservice personservice = new personservice(this.getcontext());
log.i(tag, string.valueof(personservice.getcount()));
}
public void testdelete() throws exception{
personservice personservice = new personservice(this.getcontext());
personservice.delete(1);
}
public void testdeletemore() throws exception{
personservice personservice = new personservice(this.getcontext());
personservice.delete(new integer[]{2, 5, 6});
}
}
运行结果
下一篇: IE11版本太高没法打开怎么降级呢?