您的位置:首页 > 移动开发 > Android开发

android连接SQLite数据库-----增加改查+分页

2011-01-26 21:29 176 查看
SQLite,是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了。它能够支持Windows/Linux/Unix等等主流的操作系统,同时能够跟很多程序语言相结合,比如 Tcl、C#、PHP、Java等,还有ODBC接口,同样比起Mysql、PostgreSQL这两款开源世界著名的数据库管理系统来讲,它的处理速度比他们都快。SQLite第一个Alpha版本诞生于2000年5月. 至今已经有10个年头,SQLite也迎来了一个版本 SQLite 3已经发布。

还有一件事,大家下载的时候,不要加数据库连接驱动包。本项目是不用的。

先让我们看一下图先。





业务类

package com.smart.dh;

import java.util.Iterator;

import java.util.List;

import android.test.AndroidTestCase;

import android.util.Log;

import com.smart.domain.Person;

import com.smart.service.PersonService;

public class PersonServiceTest extends AndroidTestCase {

private static final String TAG="PersonServiceTest";

//保存数据。

public void testSave() throws Exception{

PersonService personService=new PersonService(this.getContext());

// personService.save(new Person("老梁",(short)23));

for (int i = 0; i < 10; i++) {

personService.save(new Person("llb"+i,(short)(i+1)));

}

}

//查询

public void testFind() throws Exception{

PersonService personService=new PersonService(this.getContext());

Person person=personService.find(1);

Log.i(TAG, person.toString());

// personService.save(new Person("老梁",(short)23));

}

//更新语句

public void testUpdate() throws Exception{

PersonService personService=new PersonService(this.getContext());

Person person=personService.find(1);

person.setName("smart");

personService.update(person);

Log.i(TAG, person.toString());

}

//获得所有的条数

public void testGetCount() throws Exception{

PersonService personService=new PersonService(this.getContext());

Log.i(TAG, String.valueOf(personService.getCount()));

}

//分页功能

public void testGetScrollData() throws Exception{

PersonService personService=new PersonService(this.getContext());

List<Person> persons=personService.getScrollData(0, 20);//从0条到20条的数据

for(Person person:persons){

Log.i(TAG, person.toString());

}

}

public void testDelete() throws Exception{

PersonService personService=new PersonService(this.getContext());

personService.delete(1,2,3);//删除1.2.3三条记录

}

}

javaBean类

package com.smart.domain;

public class Person {

@Override

public String toString() {

return "personid="+personid+",name="+name+",age="+age;

}

public int personid;

public String name;

public Short age;

public int getPersonid() {

return personid;

}

public void setPersonid(int personid) {

this.personid = personid;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

// 增加一个构造器

public Person(int personid, String name, Short age) {

super();

this.personid = personid;

this.name = name;

this.age = age;

}

//创建构造器

public Person(String name, short age) {

this.name = name;

this.age = age;

}

public Short getAge() {

return age;

}

public void setAge(Short age) {

this.age = age;

}

}

数据库创建类

package com.smart.service;

import android.content.Context;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteDatabase.CursorFactory;

import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseOpenHelper extends SQLiteOpenHelper {

// 数据名称,

private static final String DBNAME = "smrtDataBase";

// 数据库版本

private static final int version = 1;

// 构造方法参数,

public DataBaseOpenHelper(Context context) {

super(context, DBNAME, null, version);

}

// 数据库创建表的名子。

@Override

public void onCreate(SQLiteDatabase db) {

db.execSQL("CREATE TABLE person (personid integer primary key autoincrement,name varchar(20),age INTEGER)");

}

// 更新方法

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

db.execSQL("EROP TABLE IF EXISTS person");

onCreate(db);

}

}

本文出自 “技术成就梦想” 博客,请务必保留此出处http://llb988.blog.51cto.com/1940549/486373
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: