SQLite数据库2

前端之家收集整理的这篇文章主要介绍了SQLite数据库2前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

sqlite数据库中另一种添删改查操作

OtherPersonService

packagecn.class3g.service;

importcn.class3g.domain.Person;

importandroid.content.ContentValues;

importandroid.content.Context;

importandroid.database.Cursor;

importandroid.database.sqlite.sqliteDatabase;

publicclassOtherPersonService{

privateDatabaseHelperdbHelper;

publicOtherPersonService(Contextcontext){

dbHelper=newDatabaseHelper(context);

}

publicvoidsave(Personperson){

sqliteDatabasedb=dbHelper.getWritableDatabase();

ContentValuesvalues=newContentValues();

values.put("name",person.getName());

values.put("age",person.getAge());

db.insert("person","name",values);//第二个参数name的作用体现在第三个参数values为空时

}

publicvoidupdate(Personperson,intid){

sqliteDatabasedb=dbHelper.getWritableDatabase();

ContentValuesvalues=newContentValues();

values.put("name",person.getAge());

db.update("person",values,"personid=?",newString[]{String.valueOf(id)});

}

publicPersonfind(intid){

sqliteDatabasedb=dbHelper.getReadableDatabase();

Cursorcursor=db.query("person",newString[]{"personid","age"},

"personid=?",newString[]{String.valueOf(id)},

null,null,null);

if(cursor.moveToNext()){

Personperson=newPerson();

person.setId(cursor.getInt(0));

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

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

returnperson;

}

returnnull;

}

publicvoiddelete(intid){

sqliteDatabasedb=dbHelper.getWritableDatabase();

db.delete("person",newString[]{String.valueOf(id)});

}

}

测试类OtherPersonServiceTest

packagecn.class3g.db;

importandroid.test.AndroidTestCase;

importandroid.util.Log;

importcn.class3g.domain.Person;

importcn.class3g.service.OtherPersonService;

importcn.class3g.service.PersonService;

publicclassOtherPersonServiceTestextendsAndroidTestCase{

publicvoidtestSave()throwsThrowable{

OtherPersonServiceservice=newOtherPersonService(this.getContext());

Personperson=newPerson();

person.setName("wusong");

person.setAge(2000);

service.save(person);

Personperson2=newPerson();

person2.setName("Tangbohu");

service.save(person2);

Personperson3=newPerson();

person3.setName("Qiuxiang");

service.save(person3);

Personperson4=newPerson();

person4.setName("Furong");

service.save(person4);

}

publicvoidtestUpdate()throwsThrowable{

OtherPersonServiceps=newOtherPersonService(this.getContext());

Personperson=newPerson("Jiangzhongzheng",122);

ps.update(person,5);//需要实现查看数据库Tonid

}

publicvoidtestFind()throwsThrowable{

OtherPersonServiceps=newOtherPersonService(this.getContext());

Personperson=ps.find(3);//测之前先注意被测记录id是否存在

Log.i("TAG",person.toString());

}

publicvoidtestDelete()throwsThrowable{

OtherPersonServiceps=newOtherPersonService(this.getContext());

ps.delete(1); //测之前先注意被测记录id是否存在

}

}

使用列表显示数据库数据

资源

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

<resources>

<stringname="app_name">Android_db_test</string>

<stringname="id_text">编号</string>

<stringname="name_text">姓名</string>

<stringname="age_text">年龄</string>

</resources>

布局

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

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

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical">

<RelativeLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content">

<TextView

android:id="@+id/idTitle"

android:layout_width="80dp"

android:layout_height="wrap_content"

android:text="@string/id_text"/>

<TextView

android:id="@+id/nameTitle"

android:layout_width="200dp"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/idTitle"

android:layout_alignTop="@id/idTitle"

android:gravity="center"

android:text="@string/name_text"/>

<TextView

android:id="@+id/ageTitle"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/nameTitle"

android:layout_alignTop="@id/nameTitle"

android:text="@string/age_text"/>

</RelativeLayout>

<ListView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:id="@+id/personList"

/>

</LinearLayout>

ListView布局

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

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

android:layout_width="fill_parent"

android:layout_height="wrap_content">

<TextView

android:id="@+id/id"

android:layout_width="80dp"

android:layout_height="wrap_content"/>

<TextView

android:id="@+id/name"

android:layout_width="200dp"

android:layout_height="wrap_content"

android:layout_alignTop="@id/id"

android:layout_toRightOf="@id/id"

android:gravity="center"/>

<TextView

android:id="@+id/age"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_alignTop="@id/name"

android:layout_toRightOf="@id/name"

android:gravity="left"/>

</RelativeLayout>

Activity

packagecn.class3g.db;

importjava.util.ArrayList;

importjava.util.HashMap;

importjava.util.List;

importcn.class3g.domain.Person;

importcn.class3g.service.PersonService;

importandroid.app.Activity;

importandroid.database.Cursor;

importandroid.os.Bundle;

importandroid.widget.ListView;

importandroid.widget.SimpleAdapter;

importandroid.widget.SimpleCursorAdapter;

publicclassMainActivityextendsActivity{

privateListViewlistView;

privatePersonServiceservice;

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

this.setContentView(R.layout.main);

listView=(ListView)this.findViewById(R.id.personList);

service=newPersonService(this);

/*

*//使用SimpleAdapterList<Person>personList=service.getScrollData(3,

*5);List<HashMap<String,String>>data=new

*ArrayList<HashMap<String,String>>();

*

*for(Personp:personList){HashMap<String,String>map=new

*HashMap<String,String>();map.put("personid",

*String.valueOf(p.getId()));map.put("name",p.getName());

*map.put("age",String.valueOf(p.getAge()));

*

*data.add(map);}

*

*SimpleAdapteradapter=newSimpleAdapter(this,data,

*R.layout.person_item,new

*int[]{R.id.id,R.id.name,R.id.age});

*/

//使用SimpleCursorAdapter

Cursorcursor=service.getCursorScrollData(3,5);

//注意此时绑定的cursor中的personidas_id,不能用personid

SimpleCursorAdapteradapter=newSimpleCursorAdapter(this,

R.layout.person_item,cursor,

newString[]{"_id",

newint[]{R.id.id,R.id.age});

listView.setAdapter(adapter);

}

}

猜你在找的Sqlite相关文章