android——CRUD(在上一篇博客的基础上)
来源:互联网 发布:淘宝零食店策划书 编辑:程序博客网 时间:2024/06/05 14:22
1、Person
package com.njupt.sqlite;public class Person {private Integer id;private String name;private Integer balance;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getBalance() {return balance;}public void setBalance(Integer balance) {this.balance = balance;}@Overridepublic String toString() {return "Person [id=" + id + ", name=" + name + ", balance=" + balance+ "]";}public Person(Integer id, String name, Integer balance) {super();this.id = id;this.name = name;this.balance = balance;}public Person() {super();}}
2、PersonDao
package com.njupt.sqlite;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;public class PersonDao {private DBOpenHelper helper;public PersonDao(Context context){helper = new DBOpenHelper(context);}public void insert(Person p){SQLiteDatabase db = helper.getWritableDatabase();db.execSQL("insert into person1(name,balance) values(?,?)",new Object[]{p.getName(),p.getBalance()}); db.close();}public void delete(int id){ SQLiteDatabase db = helper.getWritableDatabase(); db.execSQL("delete from person1 where id = ?",new Object[]{id}); db.close();}public void update(Person p){SQLiteDatabase db = helper.getWritableDatabase();db.execSQL("update person1 set name = ? , balance = ? where id = ?", new Object[]{p.getName(),p.getBalance(),p.getId()});db.close();}public Person query(int id){SQLiteDatabase db = helper.getReadableDatabase();Cursor c = db.rawQuery("select name ,balance from person1 where id = ?", new String[]{id + ""});Person p = null;if(c.moveToNext()){String name = c.getString(0);int balance = c.getInt(1);p = new Person(id,name,balance);}return p;} }
3、DBTest
以下附上测试类
package com.njupt.sqlite;import android.test.AndroidTestCase;public class DBTest extends AndroidTestCase{public void test1(){DBOpenHelper helper = new DBOpenHelper(getContext());helper.getWritableDatabase();}public void testInsert(){PersonDao dao = new PersonDao(getContext());Person p = new Person(3,"章泽天",45000);dao.insert(p);}public void testDelete(){PersonDao dao = new PersonDao(getContext());int id = 1;dao.delete(id);}public void testUpdate(){PersonDao dao = new PersonDao(getContext());Person p = new Person(2,"lss",40000);dao.update(p);}public void testQuery(){PersonDao dao = new PersonDao(getContext());Person p = dao.query(3);System.out.println(p);}}
4、最后我们可以在SQLiteExpert中看到相应的变化
- android——CRUD(在上一篇博客的基础上)
- 在上一篇的基础上的追加——关于使用plsql developer的数据库表的导入和导
- 在上一篇文章的基础上加了友元
- 线段树(第一篇博客,在转载的基础上做了下注释)
- Android——在源代码的基础上开发应用程序
- 在CSDN上的第一篇博客
- 在csdn上的第一篇博客
- 在csdn上的第一篇博客
- 在SCDN上的第一篇博客
- 我在CSDN上的的第一篇博客
- 没有笔在CSDN上的第一篇博客
- 我在CSDN上的第一篇博客
- 我在CSDN上的第一篇博客
- 我在csdn上的第一篇博客
- 我在csdn上写的第一篇博客
- 我在CSDN上的第一篇博客
- 我在csdn上的第一篇博客
- 上一篇博客的补充
- 数组里的正数和负数排序
- 常见排序算法总结【转载+整合】
- <<C语言深度剖析>>学习笔记之六之内存管理
- Gray-level Co-occurrence Matrix(灰度共生矩阵)
- vc将字符串表示的任意位数字进行小数位保留和四舍五入
- android——CRUD(在上一篇博客的基础上)
- 10种排序算法总结
- KL25用SPI操作nor flash
- jdk和jre有什么区别?
- js定时持行函数
- 希望能得到一些大神的指点,加QQ群:249627436 java技术交流群
- 排序算法总结
- wxPython菜单与工具栏基础示例
- js字符串常用判断方法