Android--SQLite(增,删,改,查)操作实例代码
需要5个类:
1.实体类:person.java
2.抽象类:sqloperate.java(封装了对数据库的操作)
3.助手类:dbopenhelper.java(继承sqliteopenhelper)
4.实现类:sqloperateimpl.java(实现抽象类sqloperate.java)
5.测试类:test.java(继承androidtestcase)
1.person.java
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
package com.mrzhu.sqltite;
import java.util.list;
/**
* 增删改查
* @author zlq
*
*/
public interface 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
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
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();
values.put(dbopnehelper._id, p.getid());
values.put(dbopnehelper.name, p.getname());
db.update(dbopnehelper.student_table, values, dbopnehelper._id + "=?", new string[]{string.valueof(p.getid())});
}
/**
* 查,查询表中所有的数据
*/
public list<person> find() {
list<person> persons = null;
sqlitedatabase db = dbopenhelper.getreadabledatabase();
cursor cursor = db.query(dbopnehelper.student_table, null, null, null, null, null, null);
if(cursor != null){
persons = new arraylist<person>();
while(cursor.movetonext()){
person person = new person();
int _id = cursor.getint(cursor.getcolumnindex(dbopnehelper._id));
string name = cursor.getstring(cursor.getcolumnindex(dbopnehelper.name));
person.setid(_id);
person.setname(name);
persons.add(person);
}
}
return persons;
}
/**
* 查询指定id的数据
*/
public person findbyid(int id) {
sqlitedatabase db = dbopenhelper.getreadabledatabase();
cursor cursor = db.query(dbopnehelper.student_table, null, dbopnehelper._id + "=?", new string[]{string.valueof(id)}, null, null, null);
person person = null;
if(cursor != null && cursor.movetofirst()){
person = new person();
int _id = cursor.getint(cursor.getcolumnindex(dbopnehelper._id));
string name = cursor.getstring(cursor.getcolumnindex(dbopnehelper.name));
person.setid(_id);
person.setname(name);
}
return person;
}
}
5.test.java
在androidmanifest.xml中的<application></application>外添加
(targetpackage是当前工程的包名)
<instrumentation
android:targetpackage="com.mrzhu.sqltite"
android:name="android.test.instrumentationtestrunner">
</instrumentation>
在<application></application>中添加<uses-library android:name="android.test.runner"/>
package com.mrzhu.sqltite;
import java.util.list;
import android.test.androidtestcase;
import android.util.log;
public class test extends androidtestcase {
public void testadd() throws exception{
sqloperateimpl test = new sqloperateimpl(getcontext());
person person = new person(2, "peter");
test.add(person);
}
public void testdelete() throws exception{
sqloperateimpl test = new sqloperateimpl(getcontext());
test.delete(1);
}
public void testupdata() throws exception{
sqloperateimpl test = new sqloperateimpl(getcontext());
person person = new person(1, "tom");
test.updata(person);
}
public void testfind() throws exception{
sqloperateimpl test = new sqloperateimpl(getcontext());
list<person> persons = test.find();
for (person person : persons) {
log.i("system.out", person.tostring());
}
}
public void testfindbyid() throws exception{
sqloperateimpl test = new sqloperateimpl(getcontext());
person person = test.findbyid(2);
log.i("system.out", person.tostring());
}
}