Android(三)数据存储之三SQLite嵌入式数据库 2
4.编写ContactsService类ContactsService类主要实现对业务逻辑和数据库的操作。
package com.changcheng.sqlite.service;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import com.changcheng.sqlite.MyOpenHelper;
import com.changcheng.sqlite.entity.Contact;
public class ContactsService {
private MyOpenHelper openHelper;
public ContactsService(Context context) {
this.openHelper = new MyOpenHelper(context);
}
/**
* 保存
*
* @param contact
*/
public void save(Contact contact) {
String sql = "INSERT INTO contacts (name, phone) VALUES (?, ?)";
Object[] bindArgs = { contact.getName(), contact.getPhone() };
this.openHelper.getWritableDatabase().execSQL(sql, bindArgs);
}
/**
* 查找
*
* @param id
* @return
*/
public Contact find(Integer id) {
String sql = "SELECT _id,name, phone FROM contacts WHERE _id=?";
String[] selectionArgs = { id + "" };
Cursor cursor = this.openHelper.getReadableDatabase().rawQuery(sql,
selectionArgs);
if (cursor.moveToFirst())
return new Contact(cursor.getInt(0), cursor.getString(1), cursor
.getString(2));
return null;
}
/**
* 更新
*
* @param contact
*/
public void update(Contact contact) {
String sql = "UPDATE contacts SET name=?, phone=? WHERE _id=?";
Object[] bindArgs = { contact.getName(), contact.getPhone(),
contact.get_id() };
this.openHelper.getWritableDatabase().execSQL(sql, bindArgs);
}
/**
* 删除
*
* @param id
*/
public void delete(Integer id) {
String sql = "DELETE FROM contacts WHERE _id=?";
Object[] bindArgs = { id };
this.openHelper.getReadableDatabase().execSQL(sql, bindArgs);
}
/**
* 获取记录数量
*
* @return
*/
public long getCount() {
String sql = "SELECT count(*) FROM contacts";
Cursor cursor = this.openHelper.getReadableDatabase().rawQuery(sql,
null);
cursor.moveToFirst();
return cursor.getLong(0);
}
/**
* 获取分页数据
*
* @param startIndex
* @param maxCount
* @return
*/
public List<Contact> getScrollData(long startIndex, long maxCount) {
String sql = "SELECT _id,name,phone FROM contacts LIMIT ?,?";
String[] selectionArgs = { String.valueOf(startIndex),
String.valueOf(maxCount) };
Cursor cursor = this.openHelper.getReadableDatabase().rawQuery(sql,
selectionArgs);
List<Contact> contacts = new ArrayList<Contact>();
while (cursor.moveToNext()) {
Contact contact = new Contact(cursor.getInt(0),
cursor.getString(1), cursor.getString(2));
contacts.add(contact);
}
return contacts;
}
/**
* 获取分页数据,提供给SimpleCursorAdapter使用。
*
* @param startIndex
* @param maxCount
* @return
*/
public Cursor getScrollDataCursor(long startIndex, long maxCount) {
String sql = "SELECT _id,name,phone FROM contacts LIMIT ?,?";
String[] selectionArgs = { String.valueOf(startIndex),
String.valueOf(maxCount) };
Cursor cursor = this.openHelper.getReadableDatabase().rawQuery(sql,
selectionArgs);
return cursor;
}
}
5.编写测试类
编写一个针对ContactsService的测试类,测试ContactsService类中的各个方法是否正确。
package com.changcheng.sqlite.test;
import java.util.List;
import com.changcheng.sqlite.MyOpenHelper;
import com.changcheng.sqlite.entity.Contact;
import com.changcheng.sqlite.service.ContactsService;
import android.database.Cursor;
import android.test.AndroidTestCase;
import android.util.Log;
public class ContactsServiceTest extends AndroidTestCase {
private static final String TAG = "ContactsServiceTest";
// 测试创建表
public void testCreateTable() throws Throwable {
MyOpenHelper openHelper = new MyOpenHelper(this.getContext());
openHelper.getWritableDatabase();
}
// 测试save
public void testSave() throws Throwable {
ContactsService contactsService = new ContactsService(this.getContext());
Contact contact1 = new Contact(null, "tom", "13898679876");
Contact contact2 = new Contact(null, "lili", "13041094909");
Contact contact3 = new Contact(null, "jack", "13504258899");
Contact contact4 = new Contact(null, "heary", "1335789789");
contactsService.save(contact1);
contactsService.save(contact2);
contactsService.save(contact3);
contactsService.save(contact4);
}
// 测试find
public void testFind() throws Throwable {
ContactsService contactsService = new ContactsService(this.getContext());
Contact contact = contactsService.find(1);
Log.i(TAG, contact.toString());
}
// 测试update
public void testUpdate() throws Throwable {
ContactsService contactsService = new ContactsService(this.getContext());
Contact contact = contactsService.find(1);
contact.setPhone("1399889955");
contactsService.update(contact);
}
// 测试getCount
public void testGetCount() throws Throwable {
ContactsService contactsService = new ContactsService(this.getContext());
Log.i(TAG, contactsService.getCount() + "");
}
// 测试getScrollData
public void testGetScrollData() throws Throwable {
ContactsService contactsService = new ContactsService(this.getContext());
List<Contact> contacts = contactsService.getScrollData(0, 3);
Log.i(TAG, contacts.toString());
}
// 测试getScrollDataCursor
public void testGetScrollDataCursor() throws Throwable {
ContactsService contactsService = new ContactsService(this.getContext());
Cursor cursor = contactsService.getScrollDataCursor(0, 3);
while (cursor.moveToNext()) {
Contact contact = new Contact(cursor.getInt(0),
cursor.getString(1), cursor.getString(2));
Log.i(TAG, contact.toString());
}
}
}
启用测试功能,不要忘记在AndroidManifest.xml文件中加入测试环境。为application元素添加一个子元素:<uses-library android:name="android.test.runner"/>,为application元素添加一个兄弟元素:<instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.changcheng.sqlite" android:label="Tests for My App" />。
SQLite数据库以单个文件存储,就像微软的Access数据库。有一个查看SQLite数据库文件的工具——SQLite Developer,我们可以使用它来查看数据库。Android将创建的数据库存放在”/data/data/ com.changcheng.sqlite/databases/contacts”,我们将它导出然后使用SQLite Developer打开。
6.分页显示数据
我们在ContactsService类中,提供了一个获取分页数据的方法。我们将调用它获取的数据,使用ListView组件显示出来。
编辑mail.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- ListView -->
<ListView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/listView" />
</LinearLayout>
页:
[1]