Android——另外一种增删查改的方式(ContentProvider常用)

以下介绍另外一种增删查改的方式

 

package com.njupt.sqllist;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class PersonDao {

	DBOpenHelper helper;

	public PersonDao(Context context) {
		helper = new DBOpenHelper(context);
	}

	public void insert(Person p) {
		SQLiteDatabase db = helper.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		values.put("name", p.getName());
		values.put("balance", p.getBalance());
		long row = db.insert("person", null, values);
		System.out.println("在第"+row+"插入");
		db.close();
	}

	public void delete(int id) {
		SQLiteDatabase db = helper.getWritableDatabase();
		
		int rows = db.delete("person", "id = ? ", new String[]{ id + ""});
		
		System.out.println("删除了"+rows+"行");
		db.close();
	}

	public void update(Person p) {
		SQLiteDatabase db = helper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("name", p.getName());
		values.put("balance", p.getBalance());
		int rows = db.update("person", values,"id = ?",new String[]{p.getId() + ""});
		
		System.out.println("更新了"+rows+"行");
		db.close();
	}

	public Person query(int id) {
		SQLiteDatabase db = helper.getReadableDatabase();
		
		
		Cursor c = db.query("person", new String[]{"name,balance"}, "id = ? ", new String[]{id + ""}, null, null, null);
		Person p = null;
		if (c.moveToNext()) {
			String name = c.getString(c.getColumnIndex("name"));
			int balance = c.getInt(1);

			p = new Person(id, name, balance);
		}

		System.out.println("这是一种另一种方式进行查询");
		return p;
	}

	public List<Person> queryAll() {
		SQLiteDatabase db = helper.getReadableDatabase();
		
		Cursor c = db.query("person", null, null, null, null, null, null);
		
		List<Person> persons = new ArrayList<Person>();

		while (c.moveToNext()) {
			Person p = new Person(c.getInt(0), c.getString(1), c.getInt(2));

			persons.add(p);
		}

		System.out.println("这时再用另外一种方式查询所有");
		return persons;
	}

	public void remit(int from, int to, int count) {
		SQLiteDatabase db = helper.getWritableDatabase();
		try {
			db.beginTransaction();

			db.execSQL("update person set balance = balance - ? where id = ?",
					new Object[] { count, from });
			System.out.println(1 / 0);
			db.execSQL("update person set balance = balance + ? where id = ?",
					new Object[] { count, to });

			db.setTransactionSuccessful();
		} finally {

			db.endTransaction();
			db.close();
		}
	}

	public int queryCount() {
		SQLiteDatabase db = helper.getReadableDatabase();

		Cursor c = db.query("person", new String[]{"count(*)"}, null, null,null, null, null);
		c.moveToNext();

		int count = c.getInt(0);

		System.out.println("这时再用另外一种方式查询总数");
		return count;
	}

	public List<Person> queryPage(int pageNum, int pageSize) {
		SQLiteDatabase db = helper.getReadableDatabase();

		Cursor c = db.query("person",null, null, null,null,null,null, (pageNum - 1) * pageSize + " , " + pageSize);
		List<Person> persons = new ArrayList<Person>();

		while (c.moveToNext()) {
			Person p = new Person(c.getInt(0), c.getString(1), c.getInt(2));

			persons.add(p);
		}

		System.out.println("这时再用另外一种方式在分页");
		return persons;
	}

}


 

 

你可能感兴趣的:(ContentProvider)