废话就不多说了,开始。。。
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; } @Override public 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中看到响应的变化
文章结束给大家分享下程序员的一些笑话语录: 人脑与电脑的相同点和不同点,人脑会记忆数字,电脑也会记忆数字;人脑会记忆程序,电脑也会记忆程序,但是人脑具有感知能力,这种能力电脑无法模仿,人的记忆会影响到人做任何事情,但是电脑只有程序软件。比尔还表示,人脑与电脑之间最重要的一个差别就是潜意识。对于人脑存储记忆的特别之处,比尔表示,人脑并不大,但是人脑重要的功能是联络,人脑会把同样的记忆存储在不同的地方,因此记忆读取的速度就不相同,而这种速度取决于使用的频率和知识的重要性。人脑的记忆存储能力会随着年龄增长而退化,同时记忆的质量也会随着年龄退化。经典语录网
--------------------------------- 原创文章 By
crud和android
---------------------------------