Package Com.examp.use_sqlite.dao;import Java.util.arraylist;import Java.util.list;import Android.content.contentvalues;import Android.content.context;import Android.database.cursor;import Android.database.sqlite.sqlitedatabase;import Com.examp.use_sqlite.personsqliteopenhelper;import Com.examp.use_ Sqlite.domain.person;public class Persondaoforandroid {//Gets the database object to manipulate private personsqliteopenhelper helper;/** * The helper initialization is completed in the construction method * * @param context */public persondaoforandroid (context context) {helper = new Personsqliteopenhelper ( context);} /** * Add Data * * @param name name * @param number Phone * @return returns the row ID of the newly inserted row, or-1 indicates an error */public long Add (string name, String n umber) {//Gets the connection object of the database Sqlitedatabase db = Helper.getwritabledatabase ();//The following is the API recommended notation contentvalues values = new Contentvalues (); Values.put ("name", name), Values.put ("number", number),///Target data table//returns the row ID of the newly inserted row, or-1 indicates an error occurred long id = Db.insert ("person", null, values);//Close database connection Db.close (); return ID;} /** * Query data based on user name * * @param name * Query condition * @return Query result */public person Findbyname (String name) {//Get database connection Sqlitedatabase db = Helper.getreadabledatab ASE ();//Target data table name | Specifies the data field that is returned, assuming that null means that all data fields are returned | Conditional statements, List of parameters that do not contain wherekeyword| conditions | Grouped statements |having QUERY statements | sorted statements, Assume that you do not set the default cursor cursor = db.query ("person", NULL, "Name=?", new string[] {name}, NULL, NULL, NULL), or declare the result object person pers On = null;//infers if there is a result that returns the IF (Cursor.movetonext ()) {//////Cursor.getcolumnindex ("id") of the field data for the single row of data in the data set; Gets the subscript for the specified field// Cursor.getint (index), or GetString (index); Is the data that gets the specified subscript index to int type; int id = cursor.getint (cursor.getcolumnindex ("id")) ; String named = cursor.getstring (Cursor.getcolumnindex ("name")); String number = cursor.getstring (Cursor.getcolumnindex ("number"));//Instantiate query result data Object person = new person (ID, named, number);} Close the database connection, release the Resource Db.close ();//Return the data result object return person; /** * Update a data based on name * * @param name * with new conditions * @param newnumber * NEW data * @return rows affected rows */public I NT Update (string name, String newnumber) {Sqlitedatabase db = HELPER.GETWRitabledatabase (); Contentvalues values = new Contentvalues () values.put ("number", newnumber);//Target data table | The data to be updated | Update CONDITION statement | update condition list int rows = Db.update ("person", Values, "Name=", new string[] {name});d b.close (); return rows;} /** * Delete Data according to the specified name * * @param name * Delete condition * @return rows affected rows */public int Delete (String name) {Sqlitedatabase db = Helper.getwritabledatabase ();//target database table name | Conditional statement | condition parameter int rows = db.delete ("Person", "Name=?", new string[] {name});d b . Close (); return rows;} /** * Query All operations * * @return The result set of the query */public list<person> findAll () {Sqlitedatabase db = Helper.getreadabledatabase (); cursor cursor = db.query ("person", NULL, NULL, NULL, NULL, NULL, NULL);//Declare result set//Initialize result set list<person> persons = new A Rraylist<person> ();//Infer if there is a result return while (Cursor.movetonext ()) {//Declaration result Object Person man = null;//take out data from a single row of data in the dataset/ /Cursor.getcolumnindex ("id"), gets the subscript//Cursor.getint (index) of the specified field, or GetString (index); Is the data that gets the specified subscript index to int type; int id = Cursor.getint (Cursor.getcolumnindex ("ID ")); String named = cursor.getstring (Cursor.getcolumnindex ("name")); String number = cursor.getstring (Cursor.getcolumnindex ("number"));//instantiation of query result data Object person = new person (ID, named, number) ;//Add Data Persons.add (person) to the result set;} Close the connection, release the Resource Db.close ();//Put back the result set return persons;}}
other code see also a blog Android implementation of the SQLite database of the addition, deletion, change, check the operation