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 ();}}}