|
sqlite属于一种嵌入式的数据库,并不像其他数据库要安装服务器端,只要安装android模拟器就可以对sqlite操作了。
首先写一个数据库的工具类 import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseHepler extends SQLiteOpenHelper {
private static final String NAME = "sharp.db";
private static final int version = 1;
public DataBaseHepler(Context context) {
super(context, NAME, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table person(personid integer primary key autoincrement,name varchar(20),age integer)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
复制代码只需要在里面添加表就可以了
然后添加一个对象 public class Person {
private Integer id;
private String name;
private Integer age;
public Person(Integer id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Person(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public Person() {
super();
}
public String toString(){
return "id = " + id + " name = " + name + " age = " + age;
}
}
复制代码之后写一个service类,里面包含数据库的一些基本的操作 import java.util.ArrayList;
import java.util.List;
import com.sharpandroid.domain.Person;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class PersonService {
private Context context;
private DataBaseHepler dataBaseHepler;
public PersonService(Context context) {
this.context = context;
dataBaseHepler = new DataBaseHepler(context);
}
public void save(Person person){
SQLiteDatabase database = dataBaseHepler.getWritableDatabase();
database.execSQL("insert into person(name,age) values(?,?)",new Object[]{person.getName(),person.getAge()});
}
public void update(Person person){
SQLiteDatabase database = dataBaseHepler.getWritableDatabase();
database.execSQL("update person set name=?,age=? where personid=?",new Object[]{person.getName(),person.getAge(),person.getId()});
}
public Person find(Integer id){
SQLiteDatabase database = dataBaseHepler.getWritableDatabase();
Cursor cursor = database.rawQuery("select personid,name,age from person where personid=?", new String[]{String.valueOf(id)});
if(cursor.moveToNext()){
Person person = new Person();
person.setId(cursor.getInt(cursor.getColumnIndex("personid")));
person.setName(cursor.getString(1));
person.setAge(cursor.getInt(2));
return person;
}
cursor.close();
return null;
}
public void delete(Integer id){
SQLiteDatabase database = dataBaseHepler.getWritableDatabase();
database.execSQL("delete from person where personid=?",new Object[]{id});
}
public long getCount(){
SQLiteDatabase database = dataBaseHepler.getWritableDatabase();
Cursor cursor = database.rawQuery("select count(*) from person", null);
cursor.moveToFirst();
long count = cursor.getLong(0);
cursor.close();
return count;
}
}
复制代码在AndroidManifest.xml添加测试类 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.activity"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="android.test.runner"/>
<activity android:name=".PersonActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.sharpandroid.activity" android:label="Test for My App"/>
</manifest>
复制代码之后写一个测试单元就可以进行测试了 public class PersonServiceTest extends AndroidTestCase {
public void testSave() throws Throwable{
PersonService personService = new PersonService(this.getContext());
Person person = new Person("Tom",21);
personService.save(person);
}
}
复制代码要想看执行之后生成的数据库可以用File Explorer视图就可以看到生成之后的数据库文件,可以把生成之后的数据库导出来导入到sqlite可视化工具,就可以查看里面的数据,也可以用adb命令查看表中的数据 |
|