Android uses the xUtils framework to add, delete, modify, and query sqllite.

Source: Internet
Author: User

Android uses the xUtils framework to add, delete, modify, and query sqllite.

First download xUtils,: https://github.com/wyouflf/xUtils compressed downloaded files, copy the jar package into the project:

Create a User class to test, add, delete, modify, and query

package com.example.xutilsdemo;public class User {    private int id;    private String name;    private String email;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getEmail() {        return email;    }    public void setEmail(String email) {        this.email = email;    }    public User(String name, String email) {        this.name = name;        this.email = email;    }    public User(){            }    @Override    public String toString() {        return "User [id=" + id + ", name=" + name + ", email=" + email + "]";    }     }

Core code:

Package com. example. xutilsdemo; import java. util. list; import com. lidroid. xutils. dbUtils; import com. lidroid. xutils. db. sqlite. selector; import com. lidroid. xutils. db. sqlite. whereBuilder; import com. lidroid. xutils. exception. dbException; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. view. view; import android. widget. toast; public class MainActivity extends Activity {// use Dbutils directly. If this class cannot be used, the package is not imported into DbUtils db; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // create a database named mydb db = DbUtils. create (this, "mydb");} // Save the data event public void saveClick (View v) {DbUtils db = DbUtils. create (this, "mydb"); User user = new User ("hyy", "272958293@qq.com"); // note that the User object must have the id attribute, or try {db. save (user); Toast. makeText (this, "OK", Toast. LENGTH_SHORT ). show ();} catch (DbException e) {// catch Block e automatically generated by TODO. printStackTrace ();} // when saveBindingId is used to save an object, it will assign a value to the Object id} // query the data event public void queryClick (View v) {try {List <User> list = db. findAll (User. class); for (User user: list) {System. out. println (user) ;}} catch (DbException e) {// catch Block e automatically generated by TODO. printStackTrace () ;}}// query the event public void queryByClick (View v) {try {User user User = db based on conditions. findFirst (Selector. from (User. class ). where ("name", "=", "hyy"); Toast. makeText (this, user. toString (), Toast. LENGTH_SHORT ). show ();} catch (DbException e) {// catch Block e automatically generated by TODO. printStackTrace () ;}}// update data event public void updateClick (View v) {User user User = new user (); User. setId (1); user. setName ("huyuan"); user. setEmail ("272958293@qq.com"); try {db. update (user, "name", "email");} catch (DbException e) {// catch Block e automatically generated by TODO. printStackTrace () ;}}// Delete event public void deleteClick (View v) {try {db. delete (User. class, WhereBuilder. B ("id", "=", "1");} catch (DbException e) {// catch Block e automatically generated by TODO. printStackTrace ();}}}

 

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.