Android sqlLite 数据库的增删查改操作

1:PersonDao

package cn.itcast.db.dao;

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

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import cn.itcast.db.MyDBOpenHelper;
import cn.itcast.db.domain.Person;

public class PersonDao {
	private static final String TAG = "PersonDao";
	private MyDBOpenHelper dbOpenHelper;

	// 在personDao被new出来的时候 就完成初始化

	public PersonDao(Context context) {
		dbOpenHelper = new MyDBOpenHelper(context);
		// dbOpenHelper.getReadableDatabase()
		// dbOpenHelper.getWritableDatabase()
	}

	// 增删改查

	/**
	 * 往数据库添加一条数据
	 */
	public void add(String name, String phone) {
		boolean result = find(name);
		if (result)
			return;

		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		if (db.isOpen()) {
			db.execSQL("insert into person (name,phone) values (?,?)",
					new Object[] { name, phone });
			// 关闭数据库 释放数据库的链接
			db.close();
		}
	}

	/**
	 * 查找数据库的操作
	 */
	public boolean find(String name) {
		boolean result = false;
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		if (db.isOpen()) {
			Cursor cursor = db.rawQuery("select * from person where name=?",
					new String[] { name });
			if (cursor.moveToFirst()) {
				int index = cursor.getColumnIndex("phone"); // 得到phone在表中是第几列
				String phone = cursor.getString(index);
				Log.i(TAG, "phone =" + phone);
				result = true;

			}
			// 记得关闭掉 cursor
			cursor.close();
			result = false;
			// 释放数据库的链接
			db.close();
		}
		return result;
	}

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

	/**
	 * 更新一条记录
	 * 
	 */
	public void update(String name, String newname, String newphone) {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		if (db.isOpen()) {
			db.execSQL("update person set name=? , phone=? where name=?",
					new Object[] { newname, newphone, name });
			db.close();
		}
	}

	/**
	 * 查找全部
	 */
	public List<Person> getAllPersons() {
		List<Person> persons=null;
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		if (db.isOpen()) {
			persons = new ArrayList<Person>();
			Cursor cursor = db.rawQuery("select * from person ", null);
			while (cursor.moveToNext()) {
				Person person = new Person();
				int nameindex = cursor.getColumnIndex("name");
				int phoneindex = cursor.getColumnIndex("phone");
				String name = cursor.getString(nameindex);
				String phone = cursor.getString(phoneindex);
				person.setName(name);
				person.setNumber(phone);
				persons.add(person);
			}
			cursor.close();
			db.close();
		}
		return persons;
	}

}

2 :TestPersonDao

package cn.itcast.db.dao.test;

import java.util.List;

import cn.itcast.db.dao.PersonDao;
import cn.itcast.db.domain.Person;
import android.test.AndroidTestCase;

public class TestPersonDao extends AndroidTestCase {
	PersonDao dao;
	// 测试在执行测试代码时候的流程 
	//1 .new TestPersonDao 框架new出来 
	//2. 调用 setUp()
	//3. testAdd()  这个时候 上下文 才被创建出来 
	
	
/*	@Override
	protected void setUp() throws Exception {
		dao = new PersonDao(getContext());
		super.setUp();
	}*/
	public void testAdd() throws Exception{
		PersonDao dao = new PersonDao(getContext());
		for(int i=0;i<100;i++){
		dao.add("lisi"+i, "123456789"+i);
		}
	}
	public void testdelete() throws Exception{
		PersonDao dao = new PersonDao(getContext());
		dao.delete("lisi99");
	}
	public void testupdate() throws Exception{
		PersonDao dao = new PersonDao(getContext());
		dao.update("lisi98", "wangwu", "119");
	}
	
	public void testFindAll() throws Exception{
		PersonDao dao = new PersonDao(getContext());
		List<Person>  persons  = dao.getAllPersons();
		assertEquals(100, persons.size());
	}
 }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: WinForm是.NET Framework下的一种GUI应用程序开发框架,SQLite是一种轻量级的嵌入式数据库引擎。使用WinForm结合SQLite实现增删查改操作可以通过以下步骤实现: 1. 引入SQLite的相关命名空间和引用 在WinForm项目中,首先需要添加SQLite的引用,可以通过NuGet包管理器引入System.Data.SQLite.Core。然后,在代码中引入SQLite的命名空间。 2. 创建SQLite数据库文件 使用SQLite的API和语法,可以创建一个SQLite数据库文件。可以使用SQLiteConnection类来连接数据库,并传入相应的数据库文件路径。 3. 创建数据表 使用SQLite的语法,可以通过SQLiteConnection类的ExecuteNonQuery方法执行创建表的SQL语句。 4. 插入数据 使用SQLite的语法,可以通过SQLiteConnection类的ExecuteNonQuery方法执行插入数据的SQL语句。 5. 查询数据 使用SQLite的语法,可以通过SQLiteDataAdapter类执行查询数据的SQL语句,并将结果存储在DataSet中。 6. 更新数据 使用SQLite的语法,可以通过SQLiteConnection类的ExecuteNonQuery方法执行更新数据的SQL语句。 7. 删除数据 使用SQLite的语法,可以通过SQLiteConnection类的ExecuteNonQuery方法执行删除数据的SQL语句。 以上就是使用WinForm结合SQLite实现增删查改操作的基本步骤。除了使用原生的SQLite语法,还可以借助ORM框架,如Entity Framework或Dapper来简化SQLite操作。另外,在编写代码时要注意异常处理、连接状态管理和安全性等方面的问题。 ### 回答2: 在WinForm中使用SQLite实现增删查改是一种简单而且有效的数据库操作方法。下面以300字回答增删查改的具体步骤: 增(Insert):首先需要在WinForm中创建SQLite数据库文件,并建立表格用于存储数据。然后,在代码中连接数据库,并使用INSERT语句将新的数据插入到表格中。 删(Delete):在WinForm中,可以通过用户输入的方式获取要删除的数据的特定标识,然后使用DELETE语句从数据库中删除相应的数据。 查(Select):通过SQLite提供的SELECT语句可以查询数据库中的数据。在WinForm中,可以根据用户的输入条件构建相应的SELECT语句,然后执行查询操作,并将获取到的数据展示在界面上。 改(Update):与删除类似,在WinForm中可以通过用户输入获取要修改的数据的特定标识,然后使用UPDATE语句更新数据库中对应的数据。 需要注意的是,在使用WinForm和SQLite进行数据库操作时,应遵循良好的编程规范,确保数据的完整性和安全性。同时,需要处理好异常情况,使用try...catch结构来处理可能出现的错误,以提高程序的稳定性。 总结起来,使用WinForm和SQLite实现增删查改的步骤包括:建立数据库和表格、连接数据库、构建相应的SQL语句、执行SQL语句、处理异常情况。这些步骤可以帮助我们高效地进行数据库操作,并提供可靠的数据存储和查询功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值