sqlite数据库之三---封装好的sql语句的增删查改

前端之家收集整理的这篇文章主要介绍了sqlite数据库之三---封装好的sql语句的增删查改前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package cn.itcast.db.dao;

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

import cn.itcast.db.MyDBOpenHelper;
import cn.itcast.db.domain.Person;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.sqliteDatabase;

public class PersonDBDao {
	private Context context;
	MyDBOpenHelper dbOpenHelper;

	public PersonDBDao(Context context) {
		this.context = context;
		dbOpenHelper = new MyDBOpenHelper(context);
	}

	/**
	 * 添加一条记录
	 */
	public void add(String name,int age) {
		sqliteDatabase db = dbOpenHelper.getWritableDatabase();
		if (db.isOpen()) {
			// db.execsql("insert into person (name,age) values (?,?)",new
			// Object[]{name,age});
			// db.execsql("insert into person ",null) // 不合法的sql语句
			ContentValues values = new ContentValues();
			values.put("name",name);
			values.put("age",age);
			// 如果 contentvalues为空
			db.insert("person",null,values); // 组拼sql语句完成的添加的操作
			
			// insert into person name values (NULL) ;
			db.close();
		}

	}

	/**
	 * 删除一条记录
	 */
	public void delete(String name) {
		sqliteDatabase db = dbOpenHelper.getWritableDatabase();
		if (db.isOpen()) {
			db.delete("person","name=?",new String[] { name });
			db.close();
		}
	}

	/**
	 * 数据库的更改操作
	 */
	public void update(String name,String newname,int newage) {
		sqliteDatabase db = dbOpenHelper.getWritableDatabase();
		if (db.isOpen()) {
			ContentValues values = new ContentValues();
			values.put("name",newname);
			values.put("age",newage);
			db.update("person",values,new String[] { name });
			db.close();
		}
	}

	/**
	 * 数据库查询操作
	 */
	public boolean find(String name) {
		boolean result = false;
		sqliteDatabase db = dbOpenHelper.getReadableDatabase();
		if (db.isOpen()) {
			// select * from person
			Cursor cursor = db.query("person",new String[] { name },null);
			if (cursor.moveToFirst()) {
				result = true;
			}
			cursor.close();
			db.close();
		}
		return result;

	}

	/**
	 * 查询所有信息
	 */
	public List<Person> findAll() {
		List<Person> persons = null;
		sqliteDatabase db = dbOpenHelper.getReadableDatabase();
		if (db.isOpen()) {
		//得到游标.
			Cursor cursor = db.query("person",null);
			persons = new ArrayList<Person>();
			while (cursor.moveToNext()) {
				Person person = new Person();
				String name = cursor.getString(cursor.getColumnIndex("name"));
				person.setName(name);
				int age = cursor.getInt(cursor.getColumnIndex("age"));
				person.setAge(age);
				persons.add(person);
			}
			cursor.close();
			db.close();
		}
		return persons;
	}
	/**
	 * 查询所有信息,游标查询的话必须用rawQuery.
	 */
	public Cursor findAllbyCursor() {
		sqliteDatabase db = dbOpenHelper.getReadableDatabase();
		if (db.isOpen()) {
			/*Cursor cursor = db.query("person",null);*/
//select personid as _id,是给personid起了一个别名.游标查询适配器的时候不用_id容易出问题.
			Cursor cursor = db.rawQuery("select personid as _id,age,name from person",null);
			
			return cursor;
			// 注意了  一定不要把数据库 关闭了 
			}
		return null;
		
	}
	/**
	 * 银行转账的方法
	 */
	public void transaction() {
		sqliteDatabase db = dbOpenHelper.getWritableDatabase();
		if (db.isOpen()) {

			try {
				// 一共四步.1开启数据库的事务
				db.beginTransaction();
				//2执行语句
				// 给张三设置1000块钱的账户
				db.execsql("update person set account=? where name=?",new Object[] { 1000,"zhangsan98" });
				// 把张三的账户扣除200块钱
				db.execsql("update person set account=account-? where name=?",new Object[] { 200,"zhangsan98" });
				// 出现了异常
				// 把张三的钱给李四
				//初始化李四账户 为 0
				db.execsql("update person set account=? where name=?",new Object[] { 0,"lisi" });
				db.execsql("update person set account=account+? where name=?","lisi" });
				//3设置事务的状态
				db.setTransactionSuccessful();
			}
			// 显示的设置事务是否成功
			catch (Exception e) {
				// TODO: handle exception
			} finally {
			//4结束事务
				db.endTransaction();
				db.close();
			}
		}
	}
}
原文链接:https://www.f2er.com/sqlite/200426.html

猜你在找的Sqlite相关文章