Add, delete, modify, and query SQLite Databases

Source: Internet
Author: User

Still, the code that is often repeated should be summarized.

1. First create a person instance:

Person. Java

package morgen.domain;public class Person {    private Integer id;    private String name;    private String phone;    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 String getPhone() {        return phone;    }    public void setPhone(String phone) {        this.phone = phone;    }    public Person(Integer id, String name, String phone) {        this.id = id;        this.name = name;        this.phone = phone;    }    public Person(String name, String phone) {        this.name = name;        this.phone = phone;    }    @Override    public String toString() {        return "Person [id=" + id + ", name=" + name + ", phone=" + phone + "]";    }    }

2. Create Database Files and tables.

Dbopenhelper. Java

package morgen.db;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;public class DBOpenHelper extends SQLiteOpenHelper {    public DBOpenHelper(Context context) {        super(context, "morgen.db", null, 2);    }    @Override    public void onCreate(SQLiteDatabase db) {        db.execSQL("CREATE TABLE person(id integer primary key autoincrement, name varchar(20))");    }    @Override    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {        db.execSQL("ALTER TABLE person ADD phone VARCHAR(12) NULL");    }}

3. Operate the database

Personservice. Java

Package Morgen. service; import Java. util. arraylist; import Java. util. list; import android. content. context; import android. database. cursor; import android. database. SQLite. sqlitedatabase; import Morgen. DB. dbopenhelper; import Morgen. domain. person; public class personservice {private dbopenhelper; Public personservice (context) {dbopenhelper = new dbopenhelper (context);}/*** save data * @ Pa Ram person */Public void save (person) {sqlitedatabase DB = dbopenhelper. getwritabledatabase (); db.exe csql ("insert into person (name, phone) values (?,?) ", New object [] {person. getname (), person. getphone ()}); // dB. close ();}/*** Delete record * @ Param ID Record ID */Public void Delete (integer ID) {sqlitedatabase DB = dbopenhelper. getwritabledatabase (); db.exe csql ("delete from person where id =? ", New object [] {ID});}/*** update record * @ Param person */Public void Update (person) {sqlitedatabase DB = dbopenhelper. getwritabledatabase (); db.exe csql ("Update person set name = ?, Phone =? Where id =? ", New object [] {person. getname (), person. getphone (), person. GETID ()});}/*** Obtain Record * @ Param ID Record ID * @ return */public person find (integer ID) {sqlitedatabase DB = dbopenhelper. getreadabledatabase (); cursor = dB. rawquery ("select * From person where id =? ", New string [] {ID. tostring ()}); If (cursor. movetofirst () {string phone = cursor. getstring (cursor. getcolumnindex ("phone"); string name = cursor. getstring (cursor. getcolumnindex ("name"); cursor. close (); return new person (ID, name, phone);} return NULL ;} /*** retrieve record by PAGE ** @ Param offset skips the previous number of records * @ Param maxresult the number of records retrieved per page * @ return */public list <person> getscrolldata (INT offset, int maxresult) {lis T <person> Persons = new arraylist <person> (); sqlitedatabase DB = dbopenhelper. getreadabledatabase (); cursor = dB. rawquery ("select * From person order by id asc limit ?,? ", New string [] {string. valueof (offset), String. valueof (maxresult)}); While (cursor. movetonext () {int id = cursor. getint (cursor. getcolumnindex ("ID"); string phone = cursor. getstring (cursor. getcolumnindex ("phone"); string name = cursor. getstring (cursor. getcolumnindex ("name"); persons. add (new person (ID, name, phone);} cursor. close (); return persons;}/*** obtain the total number of records * @ return */public long getcount () {sqlitedatabase DB = dbopenhelper. getreadabledatabase (); cursor = dB. rawquery ("select count (*) from person", null); cursor. movetofirst (); Long Count = cursor. getlong (0); cursor. close (); Return count ;}}

Related Article

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.