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

 

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

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

 

 

 

001业务类
002package com.smart.dh;
003import java.util.Iterator;
004import java.util.List;
005import android.test.AndroidTestCase;
006import android.util.Log;
007import com.smart.domain.Person;
008import com.smart.service.PersonService;
009public class PersonServiceTest extends AndroidTestCase {
010private static final String TAG="PersonServiceTest";
011  
012//保存数据。
013public void testSave() throws Exception{
014  PersonService  personService=new PersonService(this.getContext());
015//  personService.save(new Person("老梁",(short)23));
016  for (int i = 0; i < 10; i++) {
017   personService.save(new Person("llb"+i,(short)(i+1)));
018  }
019    
020    
021}
022  
023  
024//查询
025public void testFind() throws Exception{
026  PersonService  personService=new PersonService(this.getContext());
027  Person person=personService.find(1);
028  Log.i(TAG, person.toString());
029    
030//  personService.save(new Person("老梁",(short)23));
031}
032//更新语句
033public void testUpdate() throws Exception{
034  PersonService  personService=new PersonService(this.getContext());
035  Person person=personService.find(1);
036  person.setName("smart");
037  personService.update(person);
038    
039  Log.i(TAG, person.toString());
040    
041}
042//获得所有的条数
043public void testGetCount() throws Exception{
044  PersonService  personService=new PersonService(this.getContext());
045  Log.i(TAG, String.valueOf(personService.getCount()));
046    
047}
048  
049//分页功能
050public void testGetScrollData() throws Exception{
051  PersonService  personService=new PersonService(this.getContext());
052  List<Person> persons=personService.getScrollData(0, 20);//从0条到20条的数据
053  for(Person person:persons){
054   Log.i(TAG, person.toString());
055  }
056    
057    
058}
059  
060public void testDelete() throws Exception{
061  PersonService  personService=new PersonService(this.getContext());
062  personService.delete(1,2,3);//删除1.2.3三条记录
063}
064  
065  
066  
067  
068  
069  
070  
071}
072  
073javaBean类
074package com.smart.domain;
075public class Person {
076@Override
077public String toString() {
078    
079  return "personid="+personid+",name="+name+",age="+age;
080}
081public int personid;
082public String name;
083public Short age;
084public int getPersonid() {
085  return personid;
086}
087public void setPersonid(int personid) {
088  this.personid = personid;
089}
090public String getName() {
091  return name;
092}
093public void setName(String name) {
094  this.name = name;
095}
096// 增加一个构造器
097public Person(int personid, String name, Short age) {
098  super();
099  this.personid = personid;
100  this.name = name;
101  this.age = age;
102}
103//创建构造器
104public Person(String name, short age) {
105  this.name = name;
106  this.age = age;
107  
108}
109public Short getAge() {
110  return age;
111}
112public void setAge(Short age) {
113  this.age = age;
114}
115}
116  
117  
118数据库创建类
119package com.smart.service;
120import android.content.Context;
121import android.database.sqlite.SQLiteDatabase;
122import android.database.sqlite.SQLiteDatabase.CursorFactory;
123import android.database.sqlite.SQLiteOpenHelper;
124public class DataBaseOpenHelper extends SQLiteOpenHelper {
125// 数据名称,
126private static final String DBNAME = "smrtDataBase";
127// 数据库版本
128private static final int version = 1;
129// 构造方法参数,
130public DataBaseOpenHelper(Context context) {
131  super(context, DBNAME, null, version);
132}
133// 数据库创建表的名子。
134@Override
135public void onCreate(SQLiteDatabase db) {
136  db.execSQL("CREATE TABLE person (personid integer primary key autoincrement,name varchar(20),age INTEGER)");
137}
138// 更新方法
139@Override
140public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
141  db.execSQL("EROP TABLE IF EXISTS person");
142  onCreate(db);
143}
144  
145  
146  
147}

 

 刚学用这个东西,不知道在哪里上传小项目,希望大家告诉我一下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值