赵雅智_使用SQLiteDatabase提供的增删改查方法及事务

知识点详解:http://blog.csdn.net/zhaoyazhi2129/article/details/9026093

MainActivity.java,User.java,BaseDao.java,UserDao.java同上篇

UserDaoImple.java

package com.example.android_sqlite.dao.impl;

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

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

import com.example.android_sqlite.dao.UserDao;
import com.example.android_sqlite.database.DatabaseHelper;
import com.example.android_sqlite.domain.Users;

public class UserDaoImpl implements UserDao {
	// 依赖对象
	private DatabaseHelper dh;

	// 通过数据库的构造器实例化
	public UserDaoImpl(DatabaseHelper databaseHelper) {
		this.dh = databaseHelper;
	}

	@Override
	public boolean insert(Users entity) {
		// 定义返回值
		boolean flag = false;
		// 得到数据库操作对象
		SQLiteDatabase db = dh.getWritableDatabase();
		// 封装的参数
		ContentValues values = new ContentValues();
		values.put("userid", entity.getUserId());
		values.put("username", entity.getUserName());
		values.put("userage", entity.getUserAge());
		values.put("usersalary", entity.getUserSalary());

		// 参数1:表名,参数2:缺省的列的名称,参数3:插入数据
		long id = db.insert("users", null, values);
		// 对插入的返回值进行判断
		if (id != -1) {
			flag = true;
		}
		return flag;

	}

	@Override
	public boolean update(Users entity) {
		// 定义返回值
		boolean flag = false;
		// 得到数据库操作对象
		SQLiteDatabase db = dh.getWritableDatabase();
		// 封装的参数
		ContentValues values = new ContentValues();
		values.put("userid", entity.getUserId());
		values.put("username", entity.getUserName());
		values.put("userage", entity.getUserAge());
		values.put("usersalary", entity.getUserSalary());

		int num = db.update("users", values, "userid=?",
				new String[] { entity.getUserId() + "" });
		// 对插入的返回值进行判断
		if (num > 0) {
			flag = true;
		}
		return flag;
	}

	@Override
	public boolean delete(Users entity) {

		return deleteByid(entity.getUserId());
	}

	@Override
	public boolean deleteByid(Integer id) {
		// 定义返回值
		boolean flag = false;
		// 得到数据库操作对象
		SQLiteDatabase db = dh.getWritableDatabase();
		// 参数1:表名,参数2:缺省的列的名称,参数3:插入数据
		int num = db.delete("users", "userid=?", new String[] { id + "" });
		// 删除全部
		// int num = db.delete("users", null,null);
		// 对插入的返回值进行判断
		if (num > 0) {
			flag = true;
		}
		return flag;
	}

	@Override
	public Users findByid(Integer id) {
		Users entity = null;

		SQLiteDatabase db = dh.getWritableDatabase();
		Cursor c = db.query("users", new String[] { "userid", "username",
				"userage", "usersalary" }, "userid=?",
				new String[] { id + "" }, null, null, null);
		if (c.moveToNext()) {

			entity = new Users();
			entity.setUserId(c.getInt(c.getColumnIndex("userid")));
			entity.setUserName(c.getString(c.getColumnIndex("userid")));
			entity.setUserAge(c.getInt(c.getColumnIndex("userage")));
			entity.setUserSalary(c.getDouble(c.getColumnIndex("usersalary")));
		}
		return entity;
	}

	@Override
	public List<Users> findAll() {
		List<Users> entities = new ArrayList<Users>();

		SQLiteDatabase db = dh.getWritableDatabase();
		Cursor c = db.query("users", new String[] { "userid", "username",
				"userage", "usersalary" }, null, null, null, null, null);
		while (c.moveToNext()) {
			Users entity = new Users();
			entity.setUserId(c.getInt(c.getColumnIndex("userid")));
			entity.setUserName(c.getString(c.getColumnIndex("userid")));
			entity.setUserAge(c.getInt(c.getColumnIndex("userage")));
			entity.setUserSalary(c.getDouble(c.getColumnIndex("usersalary")));
			entities.add(entity);
		}

		return entities;
	}

	public void transaction() {
		SQLiteDatabase db = dh.getReadableDatabase();
		//开始事务
		db.beginTransaction();
		try {
			db.execSQL("update users set usersalary=? where userid=?",
					new Object[] { 3000, 11 });
			db.execSQL("update users set usersalary=? where userid=?",
					new Object[] { 2000, 12 });
			db.setTransactionSuccessful();// 设置事务标志为成功,在事务结束时才会提供事务,否则回滚事务

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			//如果没有成功回滚事务
			db.endTransaction();
		}

	}

}


DatabaseTese.java

package com.example.android_sqlite.test;

import java.util.List;

import com.example.android_sqlite.dao.UserDao;
import com.example.android_sqlite.dao.impl.UserDaoImpl;
import com.example.android_sqlite.database.DatabaseHelper;
import com.example.android_sqlite.domain.Users;

import android.test.AndroidTestCase;

public class DatabaseTese extends AndroidTestCase {

	public void createDatabase() {
		DatabaseHelper dh = new DatabaseHelper(getContext());
		dh.getWritableDatabase();
	}

	public void insert() {
		DatabaseHelper databaseHelper = new DatabaseHelper(getContext());
		UserDao userDao = new UserDaoImpl(databaseHelper);
		Users entity = new Users(null, "zhangsan", 20, 4000.0);
		boolean flag = userDao.insert(entity);
		assertEquals(true, flag);
	}

	public void update() {
		DatabaseHelper databaseHelper = new DatabaseHelper(getContext());
		UserDao userDao = new UserDaoImpl(databaseHelper);
		Users entity = new Users(8, "aaa", 21, 3000.0);
		boolean flag = userDao.update(entity);
		assertEquals(true, flag);
	}

	public void delete() {
		DatabaseHelper databaseHelper = new DatabaseHelper(getContext());
		UserDao userDao = new UserDaoImpl(databaseHelper);
		Users entity = new Users(6, "", null, null);
		boolean flag = userDao.delete(entity);
		assertEquals(true, flag);
	}

	public void findById() {
		DatabaseHelper databaseHelper = new DatabaseHelper(getContext());
		UserDao userDao = new UserDaoImpl(databaseHelper);
		Users entity = userDao.findByid(3);
		if (entity != null) {
			System.out.println(entity.toString());
		} else {
			System.out.println("no message");
		}

	}

	public void findAll() {
		DatabaseHelper databaseHelper = new DatabaseHelper(getContext());
		UserDao userDao = new UserDaoImpl(databaseHelper);
		List<Users> entities = userDao.findAll();
		for (Users entity : entities) {
			System.out.println(entity.toString());
		}
	}
	
	 // 有关事务的处理方式  
    public void testTransaction() {  
    	DatabaseHelper databaseHelper = new DatabaseHelper(getContext());
		UserDaoImpl userDaoImpl = new UserDaoImpl(databaseHelper);
  
       userDaoImpl.transaction();
  
    }  

}


赵雅智_使用SQLiteDatabase提供的增删改查方法及事务,布布扣,bubuko.com

赵雅智_使用SQLiteDatabase提供的增删改查方法及事务

上一篇:ASP.NET WebAPI框架解析第一篇


下一篇:应用程序使用统计信息 – .NET CORE(C#) WPF界面设计