需要5个类:
1.实体类:Person.java
2.抽象类:sqlOperate.java(封装了对数据库的 *** 作)
3.助手类:DBOpenHelper.java(继承sqliteOpenHelper)
4.实现类:sqlOperateImpl.java(实现抽象类sqlOperate.java)
5.测试类:Test.java(继承AndroIDTestCase)
@H_403_13@1.Person.java
复制代码 代码如下:@H_403_13@package com.mrzhu.sqltite;
public class Person {
private int _ID;@H_403_13@ private String name;
public int getID() {@H_403_13@ return _ID;@H_403_13@ }
public voID setID(int _ID) {@H_403_13@ this._ID = _ID;@H_403_13@ }
public String getname() {@H_403_13@ return name;@H_403_13@ }
public voID setname(String name) {@H_403_13@ this.name = name;@H_403_13@ }
@OverrIDe@H_403_13@ public String toString() {@H_403_13@ return "Person [ID=" + _ID + ",name=" + name + "]";@H_403_13@ }
public Person() {@H_403_13@ super();@H_403_13@ }
public Person(int _ID,String name) {@H_403_13@ super();@H_403_13@ this._ID = _ID;@H_403_13@ this.name = name;@H_403_13@ }@H_403_13@}@H_403_13@
2.sqlOperate.java
复制代码 代码如下:@H_403_13@package com.mrzhu.sqltite;
import java.util.List;
/**@H_403_13@ * 增删改查@H_403_13@ * @author ZLQ@H_403_13@ *@H_403_13@ */@H_403_13@public interface sqlOperate {@H_403_13@ public voID add(Person p);@H_403_13@ public voID delete(int ID);@H_403_13@ public voID updata(Person p);@H_403_13@ public List<Person> find();@H_403_13@ public Person findByID(int ID);@H_403_13@}@H_403_13@
3.DBOpenHelper.java
复制代码 代码如下:@H_403_13@package com.mrzhu.sqltite;
import androID.content.Context;@H_403_13@import androID.database.sqlite.sqliteDatabase;@H_403_13@import androID.database.sqlite.sqliteOpenHelper;
/**@H_403_13@ * 助手类@H_403_13@ * @author ZLQ@H_403_13@ *@H_403_13@ */@H_403_13@public class DBOpneHelper extends sqliteOpenHelper {
private static final int VERSION = 1;//版本@H_403_13@ private static final String DB_name = "people.db";//数据库名@H_403_13@ public static final String STUDENT_table = "student";//表名@H_403_13@ public static final String _ID = "_ID";//表中的列名@H_403_13@ public static final String name = "name";//表中的列名@H_403_13@ //创建数据库语句,STUDENT_table,_ID ,name的前后都要加空格@H_403_13@ private static final String CREATE_table = "create table " + STUDENT_table + " ( " + _ID + " Integer primary key autoincrement," + name + " text)";
public DBOpneHelper(Context context) {@H_403_13@ super(context,DB_name,null,VERSION);@H_403_13@ }
//数据库第一次被创建时调用 @H_403_13@ @OverrIDe@H_403_13@ public voID onCreate(sqliteDatabase db) {@H_403_13@ db.execsql(CREATE_table);@H_403_13@ }
//版本升级时被调用 @H_403_13@ @OverrIDe@H_403_13@ public voID onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {
}
}@H_403_13@
4.sqlOperateImpl.java
复制代码 代码如下:@H_403_13@package com.mrzhu.sqltite;
import java.util.ArrayList;@H_403_13@import java.util.List;
import androID.content.ContentValues;@H_403_13@import androID.content.Context;@H_403_13@import androID.database.Cursor;@H_403_13@import androID.database.sqlite.sqliteDatabase;
public class sqlOperateImpl implements sqlOperate{
private DBOpneHelper dbOpenHelper;
public sqlOperateImpl(Context context) {@H_403_13@ dbOpenHelper = new DBOpneHelper(context);@H_403_13@ }
/**@H_403_13@ * 增,用insert向数据库中插入数据@H_403_13@ */@H_403_13@ public voID add(Person p) {@H_403_13@ sqliteDatabase db = dbOpenHelper.getWritableDatabase();@H_403_13@ ContentValues values = new ContentValues();@H_403_13@ values.put(DBOpneHelper._ID,p.getID());@H_403_13@ values.put(DBOpneHelper.name,p.getname());@H_403_13@ db.insert(DBOpneHelper.STUDENT_table,values);@H_403_13@ }
/**@H_403_13@ * 删,通过ID删除数据@H_403_13@ */@H_403_13@ public voID delete(int ID) {@H_403_13@ sqliteDatabase db = dbOpenHelper.getWritableDatabase();@H_403_13@ db.delete(DBOpneHelper.STUDENT_table,DBOpneHelper._ID + "=?",new String[]{String.valueOf(ID)});@H_403_13@ }
/**@H_403_13@ * 改,修改指定ID的数据@H_403_13@ */@H_403_13@ public voID updata(Person p) {@H_403_13@ sqliteDatabase db = dbOpenHelper.getWritableDatabase();@H_403_13@ ContentValues values = new ContentValues();@H_403_13@ values.put(DBOpneHelper._ID,p.getname());@H_403_13@ db.update(DBOpneHelper.STUDENT_table,values,new String[]{String.valueOf(p.getID())});@H_403_13@ }
/**@H_403_13@ * 查,查询表中所有的数据@H_403_13@ */@H_403_13@ public List<Person> find() {@H_403_13@ List<Person> persons = null;@H_403_13@ sqliteDatabase db = dbOpenHelper.getReadableDatabase();@H_403_13@ Cursor cursor = db.query(DBOpneHelper.STUDENT_table,null);@H_403_13@ if(cursor != null){@H_403_13@ persons = new ArrayList<Person>();@H_403_13@ while(cursor.movetoNext()){@H_403_13@ Person person = new Person();@H_403_13@ int _ID = cursor.getInt(cursor.getColumnIndex(DBOpneHelper._ID));@H_403_13@ String name = cursor.getString(cursor.getColumnIndex(DBOpneHelper.name));@H_403_13@ person.setID(_ID);@H_403_13@ person.setname(name);@H_403_13@ persons.add(person);@H_403_13@ }@H_403_13@ }@H_403_13@ return persons;@H_403_13@ }
/**@H_403_13@ * 查询指定ID的数据@H_403_13@ */@H_403_13@ public Person findByID(int ID) {@H_403_13@ sqliteDatabase db = dbOpenHelper.getReadableDatabase();@H_403_13@ Cursor cursor = db.query(DBOpneHelper.STUDENT_table,new String[]{String.valueOf(ID)},null);@H_403_13@ Person person = null;@H_403_13@ if(cursor != null && cursor.movetoFirst()){@H_403_13@ person = new Person();@H_403_13@ int _ID = cursor.getInt(cursor.getColumnIndex(DBOpneHelper._ID));@H_403_13@ String name = cursor.getString(cursor.getColumnIndex(DBOpneHelper.name));@H_403_13@ person.setID(_ID);@H_403_13@ person.setname(name);@H_403_13@ }@H_403_13@ return person;@H_403_13@ }@H_403_13@}@H_403_13@
5.Test.java
在AndroIDManifest.xml中的<application></application>外添加
(targetPackage是当前工程的包名)
<instrumentation
androID:targetPackage="com.mrzhu.sqltite"
androID:name="androID.test.InstrumentationTestRunner">
</instrumentation>
@H_403_13@
在<application></application>中添加<uses-library androID:name="androID.test.runner"/>
复制代码 代码如下:@H_403_13@package com.mrzhu.sqltite;
import java.util.List;
import androID.test.AndroIDTestCase;@H_403_13@import androID.util.Log;
public class Test extends AndroIDTestCase {@H_403_13@ public voID testAdd() throws Exception{@H_403_13@ sqlOperateImpl test = new sqlOperateImpl(getContext());@H_403_13@ Person person = new Person(2,"Peter");@H_403_13@ test.add(person);@H_403_13@ }
public voID testDelete() throws Exception{@H_403_13@ sqlOperateImpl test = new sqlOperateImpl(getContext());@H_403_13@ test.delete(1);@H_403_13@ }
public voID testUpdata() throws Exception{@H_403_13@ sqlOperateImpl test = new sqlOperateImpl(getContext());@H_403_13@ Person person = new Person(1,"Tom");@H_403_13@ test.updata(person);@H_403_13@ }
public voID testFind() throws Exception{@H_403_13@ sqlOperateImpl test = new sqlOperateImpl(getContext());@H_403_13@ List<Person> persons = test.find();@H_403_13@ for (Person person : persons) {@H_403_13@ Log.i("System.out",person.toString());@H_403_13@ }@H_403_13@ }
public voID testFindByID() throws Exception{@H_403_13@ sqlOperateImpl test = new sqlOperateImpl(getContext());@H_403_13@ Person person = test.findByID(2);@H_403_13@ Log.i("System.out",person.toString());@H_403_13@ }@H_403_13@}@H_403_13@
以上是内存溢出为你收集整理的Android--SQLite(增,删,改,查) *** 作实例代码全部内容,希望文章能够帮你解决Android--SQLite(增,删,改,查) *** 作实例代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)