sqlite一些基本的操作

sqlite属于一种嵌入式的数据库,并不像其他数据库要安装服务器端,只要安装android模拟器就可以对sqlite操作了。

首先写一个数据库的工具类

importandroid.content.Context;

importandroid.database.sqlite.SQLiteDatabase;

importandroid.database.sqlite.SQLiteOpenHelper;

publicclassDataBaseHeplerextendsSQLiteOpenHelper{

privatestaticfinalStringname="sharp.db";

privatestaticfinalintversion=1;

publicDataBaseHepler(Contextcontext){

super(context,NAME,null,version);

}

@Override

publicvoidonCreate(SQLiteDatabasedb){

db.execSQL("createtableperson(personidintegerprimarykeyautoincrement,namevarchar(20),ageinteger)");

}

@Override

publicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion){

}

}

只需要在里面添加表就可以了

然后添加一个对象

publicclassPerson{

privateIntegerid;

privateStringname;

privateIntegerage;

publicPerson(Integerid,Stringname,Integerage){

super();

this.id=id;

this.name=name;

this.age=age;

}

publicIntegergetId(){

returnid;

}

publicvoidsetId(Integerid){

this.id=id;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

publicIntegergetAge(){

returnage;

}

publicvoidsetAge(Integerage){

this.age=age;

}

publicPerson(Stringname,Integerage){

super();

this.name=name;

this.age=age;

}

publicPerson(){

super();

}

publicStringtoString(){

return"id="+id+"name="+name+"age="+age;

}

}

之后写一个service类,里面包含数据库的一些基本的操作

importjava.util.ArrayList;

importjava.util.List;

importcom.sharpandroid.domain.Person;

importandroid.content.Context;

importandroid.database.Cursor;

importandroid.database.sqlite.SQLiteDatabase;

publicclassPersonService{

privateContextcontext;

privateDataBaseHeplerdataBaseHepler;

publicPersonService(Contextcontext){

this.context=context;

dataBaseHepler=newDataBaseHepler(context);

}

publicvoidsave(Personperson){

SQLiteDatabasedatabase=dataBaseHepler.getWritableDatabase();

database.execSQL("insertintoperson(name,age)values(?,?)",newObject[]{person.getName(),person.getAge()});

}

publicvoidupdate(Personperson){

SQLiteDatabasedatabase=dataBaseHepler.getWritableDatabase();

database.execSQL("updatepersonsetname=?,age=?wherepersonid=?",newObject[]{person.getName(),person.getAge(),person.getId()});

}

publicPersonfind(Integerid){

SQLiteDatabasedatabase=dataBaseHepler.getWritableDatabase();

Cursorcursor=database.rawQuery("selectpersonid,name,agefrompersonwherepersonid=?",newString[]{String.valueOf(id)});

if(cursor.moveToNext()){

Personperson=newPerson();

person.setId(cursor.getInt(cursor.getColumnIndex("personid")));

person.setName(cursor.getString(1));

person.setAge(cursor.getInt(2));

returnperson;

}

cursor.close();

returnnull;

}

publicvoiddelete(Integerid){

SQLiteDatabasedatabase=dataBaseHepler.getWritableDatabase();

database.execSQL("deletefrompersonwherepersonid=?",newObject[]{id});

}

publiclonggetCount(){

SQLiteDatabasedatabase=dataBaseHepler.getWritableDatabase();

Cursorcursor=database.rawQuery("selectcount(*)fromperson",null);

cursor.moveToFirst();

longcount=cursor.getLong(0);

cursor.close();

returncount;

}

}

在AndroidManifest.xml添加测试类

<?xmlversion="1.0"encoding="utf-8"?>

<manifestxmlns:android="http://schemas.android.com/apk/res/android"

package="com.android.activity"

android:versionCode="1"

android:versionname="1.0">

<applicationandroid:icon="@drawable/icon"android:label="@string/app_name">

<uses-libraryandroid:name="android.test.runner"/>

<activityandroid:name=".PersonActivity"

android:label="@string/app_name">

<intent-filter>

<actionandroid:name="android.intent.action.MAIN"/>

<categoryandroid:name="android.intent.category.LAUNCHER"/>

</intent-filter>

</activity>

</application>

<instrumentationandroid:name="android.test.InstrumentationTestRunner"android:targetPackage="com.sharpandroid.activity"android:label="TestforMyApp"/>

</manifest>

之后写一个测试单元就可以进行测试了

publicclassPersonServiceTestextendsAndroidTestCase{

publicvoidtestSave()throwsThrowable{

PersonServicepersonService=newPersonService(this.getContext());

Personperson=newPerson("Tom",21);

personService.save(person);

}

}

要想看执行之后生成的数据库可以用FileExplorer视图就可以看到生成之后的数据库文件,可以把生成之后的数据库导出来导入到sqlite可视化工具,就可以查看里面的数据,也可以用adb命令查看表中的数据

相关推荐