Add, delete, change, and check the SQLite database using the methods recommended by the API in Android

Source: Internet
Author: User
Tags sqlite database

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;/** * Completion of helper initialization 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 by 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 returned data field if NULL is set to return all data fields | Conditional statement, excluding where keyword | The argument list for the condition | Grouped statement |having QUERY statement | ordered statement, If you do not set the default cursor cursor = db.query ("person", NULL, "Name=?", new string[] {name}, NULL, NULL, NULL);//Declare result object person pers On = null;//to determine if there is a result return if (Cursor.movetonext ()) {/////Cursor.getcolumnindex ("id") of the field data of a 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 according to 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 argument 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> ();//Determine if there is a result return while (Cursor.movetonext ()) {//Declare 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 another blog Android implementation of the SQLite database to increase, delete, change, check the operation

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.