android: SQLiteOpenHelper,会将数据库放在当前应用程序的某个文件下(14)

//SQLiteOpenHelper类,要重写里面的方法
public class MySqliteOpenHelper extends SQLiteOpenHelper {
	// super(context, name, factory, version),要重写构造方法,父类参数意义
	// context上下文;name:dbName,数据库名;factory:游标工厂;version版本号,能够本身设置
	// 咱们能够本身重写,让它传入的参数变少,只用传入上下文

	// 定义一些静态参数,让它传进的参数变少
	private static final String DBNAME = "student2.db";
	// 数据库会在第一次升级软件时建立,存放地址:data/data/项目.包名/datebase()
	private static final int VERSION = 1;// 版本能够本身取

	public MySqliteOpenHelper(Context context) {
		super(context, DBNAME, null, VERSION);
		/*
		 * public MySqliteOpenHelper(Context context, String name, CursorFactory
		 * factory, int version) { super(context, name, factory, version); }
		 */
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// 建立数据库表,首次升级时调用,初始化一些数据
		String sql = "create table studentinfo "
				+ " (sid integer primary key autoincrement not null,"
				+ " sname varchar(30) not null," + " sex varchar(2) not null,"
				+ " score integer not null)";
		db.execSQL(sql);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// 大于旧版本时,就升级
		if (newVersion > oldVersion) {
			db.execSQL("drop table if exists studentinfo ");
			// 实际操做是先备份数据,再进行删除升级
		}
	}

}

//service类
/**
 * personinfo --> Person实体 --->PersonService 对Person实体的操做类(增删改查) student2.db:
 * data/data/{包名}/database/xxx.db 面向对象的封装操做类
 */
public class StudentService {
	private MySqliteOpenHelper dbhelper;

	// 除了用execSQL()进行增删改外,另外SqliteOpenHelper封装了delete(),insert(),update()方法
	// 面向对象的封装操做类
	public StudentService(Context context) {
		dbhelper = new MySqliteOpenHelper(context);
	}

	public List<Student> findAllStudents() {
		List<Student> list = new ArrayList<Student>();
		// dbHelper.getReadableDatabase() : 若是空间不足,不能写,只能读
		// dbHelper.getWritableDatabase(); 若是空间不足,既不能写,也不能读(报错)
		SQLiteDatabase db = dbhelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select*from studentinfo", null);
		while (cursor.moveToNext()) {
			// 按如下方式写时要注意数据库顺序
			int sid = cursor.getInt(0);
			String sname = cursor.getString(1);
			String sex = cursor.getString(2);
			int score = cursor.getInt(3);
			Student stu = new Student(sid, sname, sex, score);
			list.add(stu);
		}
		cursor.close();
		db.close();
		return list;
	}

	public long add(Student student) {
		SQLiteDatabase db = dbhelper.getReadableDatabase();
		ContentValues values = new ContentValues();
		values.put("sname", student.getSname());
		values.put("sex", student.getSex());
		values.put("score", student.getScore());
		long row = db.insert("studentinfo", null, values);
		// 插入成功后返回的是影响的主键
		db.close();
		return row;

	}

	public int update(ContentValues values, String whereClause,
			String[] whereArgs) {
		SQLiteDatabase db = dbhelper.getReadableDatabase();
		int row = db.update("studentinfo", values, whereClause, whereArgs);
		db.close();
		return row;// 返回的是受影响的行

	}

	public int delete(String whereClause, String[] whereArgs) {
		SQLiteDatabase db = dbhelper.getReadableDatabase();
		int row = db.delete("studentinfo", whereClause, whereArgs);
		db.close();
		return row;// 返回的是受影响的行
	}
}
//面向对象封装的实体类,记得加上setter和getter方法
public class Student {
	private int sid;
	private String sname;
	private String sex;
	private int score;

	public Student(String sname, String sex, int score) {
		this.sname = sname;
		this.sex = sex;
		this.score = score;
	}

	public Student(int sid, String sname, String sex, int score) {
		this.sid = sid;
		this.sname = sname;
		this.sex = sex;
		this.score = score;
	}
}

//测试类:要继承	AndroidTestCase
//而且在配置文件AndroidManifest.xml加进如下两项

   第一项:<!-- 引入Android单元测试框架 -->
    <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.example.andday14sqliteunittest" >
    </instrumentation>

    <application// 注意加载<application外面
   第二项:加在</application>里面
              <!-- 单元测试的依赖包 -->
        <uses-library android:name="android.test.runner" /> 

public class TestStudent extends AndroidTestCase {
	private static final String TAG = "MainActivity";

	// 添加删除等方法测试类
	public void testAdd() {
		StudentService service = new StudentService(getContext());
		Student Student = new Student("Mike", "男", 100);

		long rowid = service.add(Student);
		Log.i(TAG, "rowid : " + rowid);
	}

	public void testQuery() {
		StudentService service = new StudentService(getContext());
		List<Student> list = service.findAllStudents();
		for (Student Student : list) {
			Log.i(TAG, Student.toString());
		}
	}

	public void testUpdate() {
		StudentService service = new StudentService(getContext());
		ContentValues values = new ContentValues();

		values.put("score", 60);

		int row = service.update(values, "sid=?", new String[] { 1 + "" });
		Log.i(TAG, "row : " + row);
	}

	public void testDelete() {
		StudentService service = new StudentService(getContext());
		int row = service.delete("sid=1", null);
	}
}
相关文章
相关标签/搜索