需要5个类:
1.实体类:Person.java
2.抽象类:SQLOperate.java(封装了对数据库的操作)
3.助手类:DBOpenHelper.java(继承SQLiteOpenHelper)
4.实现类:SQLOperateImpl.java(实现抽象类SQLOperate.java)
5.测试类:Test.java(继承AndroidTestCase)
1.Person.java
[html]
package com.mrzhu.sqltite;
public class Person {
private int _id;
private String name;
public int getId() {
return _id;
}
public void setId(int _id) {
this._id = _id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person [id=" + _id + ", name=" + name + "]";
}
public Person() {
super();
}
public Person(int _id, String name) {
super();
this._id = _id;
this.name = name;
}
}
2.SQLOperate.java
[html]
package com.mrzhu.sqltite;
import java.util.List;
/**
* 增删改查
* @author ZLQ
*
*/
public inte易做图ce SQLOperate {
public void add(Person p);
public void delete(int id);
public void updata(Person p);
public List<Person> find();
public Person findById(int id);
}
3.DBOpenHelper.java
[html]
package com.mrzhu.sqltite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* 助手类
* @author ZLQ
*
*/
public class DBOpneHelper extends SQLiteOpenHelper {
private static final int VERSION = 1;//版本
private static final String DB_NAME = "people.db";//数据库名
public static final String STUDENT_TABLE = "student";//表名
public static final String _ID = "_id";//表中的列名
public static final String NAME = "name";//表中的列名
//创建数据库语句,STUDENT_TABLE,_ID ,NAME的前后都要加空格
private static final String CREATE_TABLE = "create table " + STUDENT_TABLE + " ( " + _ID + " Integer primary key autoincrement," + NAME + " text)";
public DBOpneHelper(Context context) {
super(context, DB_NAME, null, VERSION);
}
//数据库第一次被创建时调用
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
//版本升级时被调用
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
4.SQLOperateImpl.java
[html]
package com.mrzhu.sqltite;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class SQLOperateImpl implements SQLOperate{
private DBOpneHelper dbOpenHelper;
public SQLOperateImpl(Context context) {
dbOpenHelper = new DBOpneHelper(context);
}
/**
* 增,用insert向数据库中插入数据
*/
public void add(Person p) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DBOpneHelper._ID, p.getId());
values.put(DBOpneHelper.NAME, p.getName());
db.insert(DBOpneHelper.STUDENT_TABLE, null, values);
}
/**
* 删,通过id删除数据
*/
public void delete(int id) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
db.delete(DBOpneHelper.STUDENT_TABLE, DBOpneHelper._ID + "=?", new String[]{String.valueOf(id)});
}
/**
* 改,修改指定id的数据
*/
public void updata(Person p) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
ContentValues values = new ContentValues();