Android数据库基本操作 SQLite
1.Person.java
packagecom.mrzhu.sqltite;
publicclassPerson{
privateint_id;
privateStringname;
publicintgetId(){
return_id;
}
publicvoidsetId(int_id){
this._id=_id;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
@Override
publicStringtoString(){
return"Person[id="+_id+",name="+name+"]";
}
publicPerson(){
super();
}
publicPerson(int_id,Stringname){
super();
this._id=_id;
this.name=name;
}
}
2.SQLOperate.java
packagecom.mrzhu.sqltite;
importjava.util.List;
/**
*增删改查
*@authorZLQ
*
*/
publicinterfaceSQLOperate{
publicvoidadd(Personp);
publicvoiddelete(intid);
publicvoidupdata(Personp);
publicList<Person>find();
publicPersonfindById(intid);
}
3.DBOpenHelper.java
[html]
packagecom.mrzhu.sqltite;
importandroid.content.Context;
importandroid.database.sqlite.SQLiteDatabase;
importandroid.database.sqlite.SQLiteOpenHelper;
/**
*助手类
*@authorZLQ
*
*/
publicclassDBOpneHelperextendsSQLiteOpenHelper{
privatestaticfinalintVERSION=1;//版本
privatestaticfinalStringDB_name="people.db";//数据库名
publicstaticfinalStringSTUDENT_TABLE="student";//表名
publicstaticfinalString_id="_id";//表中的列名
publicstaticfinalStringname="name";//表中的列名
//创建数据库语句,STUDENT_TABLE,_ID,NAME的前后都要加空格
privatestaticfinalStringCREATE_TABLE="createtable"+STUDENT_TABLE+"("+_ID+"Integerprimarykeyautoincrement,"+NAME+"text)";
publicDBOpneHelper(Contextcontext){
super(context,DB_NAME,null,VERSION);
}
//数据库第一次被创建时调用
@Override
publicvoidonCreate(SQLiteDatabasedb){
db.execSQL(CREATE_TABLE);
}
//版本升级时被调用
@Override
publicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion){
}
}
4.SQLOperateImpl.java
packagecom.mrzhu.sqltite;
importjava.util.ArrayList;
importjava.util.List;
importandroid.content.ContentValues;
importandroid.content.Context;
importandroid.database.Cursor;
importandroid.database.sqlite.SQLiteDatabase;
publicclassSQLOperateImplimplementsSQLOperate{
privateDBOpneHelperdbOpenHelper;
publicSQLOperateImpl(Contextcontext){
dbOpenHelper=newDBOpneHelper(context);
}
/**
*增,用insert向数据库中插入数据
*/
publicvoidadd(Personp){
SQLiteDatabasedb=dbOpenHelper.getWritableDatabase();
ContentValuesvalues=newContentValues();
values.put(DBOpneHelper._ID,p.getId());
values.put(DBOpneHelper.NAME,p.getName());
db.insert(DBOpneHelper.STUDENT_TABLE,null,values);
}
/**
*删,通过id删除数据
*/
publicvoiddelete(intid){
SQLiteDatabasedb=dbOpenHelper.getWritableDatabase();
db.delete(DBOpneHelper.STUDENT_TABLE,DBOpneHelper._ID+"=?",newString[]{String.valueOf(id)});
}
/**
*改,修改指定id的数据
*/
publicvoidupdata(Personp){
SQLiteDatabasedb=dbOpenHelper.getWritableDatabase();
ContentValuesvalues=newContentValues();
values.put(DBOpneHelper._ID,p.getId());
values.put(DBOpneHelper.NAME,p.getName());
db.update(DBOpneHelper.STUDENT_TABLE,values,DBOpneHelper._ID+"=?",newString[]{String.valueOf(p.getId())});
}
/**
*查,查询表中所有的数据
*/
publicList<Person>find(){
List<Person>persons=null;
SQLiteDatabasedb=dbOpenHelper.getReadableDatabase();
Cursorcursor=db.query(DBOpneHelper.STUDENT_TABLE,null,null,null,null,null,null);
if(cursor!=null){
persons=newArrayList<Person>();
while(cursor.moveToNext()){
Personperson=newPerson();
int_id=cursor.getInt(cursor.getColumnIndex(DBOpneHelper._ID));
Stringname=cursor.getString(cursor.getColumnIndex(DBOpneHelper.NAME));
person.setId(_id);
person.setName(name);
persons.add(person);
}
}
returnpersons;
}
/**
*查询指定id的数据
*/
publicPersonfindById(intid){
SQLiteDatabasedb=dbOpenHelper.getReadableDatabase();
Cursorcursor=db.query(DBOpneHelper.STUDENT_TABLE,null,DBOpneHelper._ID+"=?",newString[]{String.valueOf(id)},null,null,null);
Personperson=null;
if(cursor!=null&&cursor.moveToFirst()){
person=newPerson();
int_id=cursor.getInt(cursor.getColumnIndex(DBOpneHelper._ID));
Stringname=cursor.getString(cursor.getColumnIndex(DBOpneHelper.NAME));
person.setId(_id);
person.setName(name);
}
returnperson;
}
}
5.Test.java
在AndroidManifest.xml中的<application></application>外添加
(targetPackage是当前工程的包名)
<instrumentation
android:targetPackage="com.mrzhu.sqltite"
android:name="android.test.InstrumentationTestRunner">
</instrumentation>
在<application></application>中添加<uses-libraryandroid:name="android.test.runner"/>
packagecom.mrzhu.sqltite;
importjava.util.List;
importandroid.test.AndroidTestCase;
importandroid.util.Log;
publicclassTestextendsAndroidTestCase{
publicvoidtestAdd()throwsException{
SQLOperateImpltest=newSQLOperateImpl(getContext());
Personperson=newPerson(2,"Peter");
test.add(person);
}
publicvoidtestDelete()throwsException{
SQLOperateImpltest=newSQLOperateImpl(getContext());
test.delete(1);
}
publicvoidtestUpdata()throwsException{
SQLOperateImpltest=newSQLOperateImpl(getContext());
Personperson=newPerson(1,"Tom");
test.updata(person);
}www.2cto.com
publicvoidtestFind()throwsException{
SQLOperateImpltest=newSQLOperateImpl(getContext());
List<Person>persons=test.find();
for(Personperson:persons){
Log.i("System.out",person.toString());
}
}
publicvoidtestFindById()throwsException{
SQLOperateImpltest=newSQLOperateImpl(getContext());
Personperson=test.findById(2);
Log.i("System.out",person.toString());
}
}