标题:
嵌入式关系型SQLite数据库存储数据 02
[打印本页]
作者:
samwalton
时间:
2013-9-7 15:43
标题:
嵌入式关系型SQLite数据库存储数据 02
然后是对学生信息的增删查改的方法类:view plain package com.jiangqq.service;
/** * 该类进行对学生信息的数据库的增删查改操作的封装*/ import java.util.ArrayList;import java.util.List;
import com.jiangqq.model.Student;
import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;
public class DBDatabase { private DBOpenHelper dbOpenHelper;private SQLiteDatabase dbDatabase;
public DBDatabase(Context context) { this.dbOpenHelper = new DBOpenHelper(context);
}
// 添加public void add(Student student) { dbDatabase = dbOpenHelper.getWritableDatabase();ContentValues values = new ContentValues();values.put("name", student.getName());values.put("gender", student.getGender());values.put("age", student.getAge());values.put("telphone", student.getTelphone());dbDatabase.insert("student", null, values);if (dbDatabase != null) { dbDatabase.close();}
// 删除操作public void delete(Integer id) { dbDatabase = dbOpenHelper.getWritableDatabase();dbDatabase.delete("student", "stu_id= ? ",new String[] { String.valueOf(id) });if (dbDatabase != null) { dbDatabase.close();}
// 修改public void update(Student student, Integer id) { dbDatabase = dbOpenHelper.getWritableDatabase();ContentValues values = new ContentValues();values.put("name", student.getName());values.put("gender", student.getGender());values.put("age", student.getAge());values.put("telphone", student.getTelphone());dbDatabase.update("student", values, "stu_id=?",new String[] { String.valueOf(id) });if (dbDatabase != null) { dbDatabase.close();}
// 查询public List<Student> findAll() { dbDatabase = dbOpenHelper.getWritableDatabase();Cursor cursor = dbDatabase.query("student", new String[] { "stu_id","name", "gender", "age", "telphone" }, null, null, null, null,"stu_id asc");List<Student> list = new ArrayList<Student>();Student student = null;while (cursor.moveToNext()) { student = new Student();student.setId(cursor.getInt(0));student.setName(cursor.getString(1));student.setGender(cursor.getString(2));student.setAge(cursor.getShort(3));student.setTelphone(cursor.getString(4));list.add(student);} if (dbDatabase != null) { dbDatabase.close();} return list;}
public Student findById(Integer id) { dbDatabase = dbOpenHelper.getWritableDatabase();Cursor cursor = dbDatabase.query("student", new String[] { "stu_id","name", "gender", "age", "telphone" }, "stu_id=?",new String[] { String.valueOf(id) }, null, null, null);int count = cursor.getCount();if (count != 0) { cursor.moveToNext();Student student = new Student(cursor.getInt(cursor.getColumnIndex("stu_id")), cursor.getString(cursor.getColumnIndex("name")), cursor.getString(cursor.getColumnIndex("gender")), cursor.getShort(cursor.getColumnIndex("age")), cursor.getString(cursor.getColumnIndex("telphone")));if (dbDatabase != null) { dbDatabase.close();} return student;} else { { return null;}
}
}
方法单元测试类:view plain package com.jiangqq.service.test;
import java.util.List;
import com.jiangqq.model.Student;import com.jiangqq.service.DBDatabase;
import android.test.AndroidTestCase;import android.util.Log;
public class SQLiteDatabaseTest extends AndroidTestCase { private static final String TAG = "SQLiteDatabaseTest";
public void testAdd() { Student student = null;DBDatabase database = new DBDatabase(this.getContext());student = new Student();student.setName("李华");student.setGender("男");student.setAge((short) 20);student.setTelphone("18355101099");database.add(student);student = new Student();student.setName("李华");student.setGender("男");student.setAge((short) 20);student.setTelphone("18355101099");database.add(student);Log.i(TAG, "添加成功");}
public void testDelete() { DBDatabase database = new DBDatabase(this.getContext());database.delete(1);Log.i(TAG, "删除成功");}
public void testUpdate() { DBDatabase database = new DBDatabase(this.getContext());Student student = new Student("李华", "男", (short) 20, "1388888888");database.update(student, 1);Log.i(TAG, "修改成功");}
public void testFindAll() { DBDatabase database = new DBDatabase(this.getContext());List<Student> list = database.findAll();for (int y = 0; y < list.size(); y++) { // System.out.println(((Student) list.get(y))。toString());Log.i(TAG, ((Student) list.get(y))。toString());}
public void testFindById() { DBDatabase database = new DBDatabase(this.getContext());Student student = database.findById(2);Log.i(TAG, student.toString());}
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0