Android-CRUD (based on the previous blog)

Source: Internet
Author: User

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

Attached test class

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, "zhangzetian", 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. Finally, we can see the corresponding changes in sqliteexpert.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.