Android in the API recommended way to achieve the growth of SQLite database, delete, change, check operation

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;/** * 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;}}


See the rest of the code, as well as a blog Android implementation of SQLite database growth, deletion, change, check operation

Android in the API recommended way to achieve the growth of SQLite database, delete, change, check operation

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.