Use the API recommended method in Android to add, delete, modify, and query SQLite Databases

Source: Internet
Author: User

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 {// obtain the database object to be operated private PersonSQLiteOpenHelper he Lper;/*** initialize helper in the constructor *** @ param context */public PersonDaoForAndroid (Context context) {helper = new PersonSQLiteOpenHelper (context );} /***** add data ** @ param name * @ param number * @ return returns the row ID of the newly inserted row, or-1 indicates an error */public long add (String name, String number) {// obtain the database connection object SQLiteDatabase db = helper. getWritableDatabase (); // The following is the API recommendation method ContentValues values = new ContentValues (); values. put ("Name", name); values. put ("number", number); // target data table // return the row ID of the newly inserted row, or-1 indicates that an error occurs long id = db. insert ("person", null, values); // close the database connection in time. close (); return id;}/*** query data by user name ** @ param name * query condition * @ return query result */public Person findByName (String name) {// obtain the database connection SQLiteDatabase db = helper. getReadableDatabase (); // target data table name | specify the returned data field. If null is set, all data fields are returned. | Condition Statement, excluding the where keyword | condition parameter list | grouping statement | having query statement | sort Language If not set, the default Cursor cursor = db. query ("person", null, "name =? ", New String [] {name}, null); // declare the result object Person person = null; // determine whether results are returned if (cursor. moveToNext () {// retrieve the data of the fields of a single row of data in the dataset // cursor. getColumnIndex ("id"); returns the subscript of the specified field // cursor. getInt (index); or getString (index); is used to obtain the data index of the specified underlying object as the int type; int id = cursor. getInt (cursor. getColumnIndex ("id"); String named = cursor. getString (cursor. getColumnIndex ("name"); String number = cursor. getString (cursor. getColum NIndex ("number"); // instantiate the query result data object person = new Person (id, named, number);} // close the database connection and release the resource database. close (); // return data result object return person ;} /*** update a piece of data based on name ** @ param name * and new condition * @ param newnumber * New Data * @ return rows affected rows */public int update (String name, string newnumber) {SQLiteDatabase db = helper. getWritableDatabase (); ContentValues values = new ContentValues (); values. put ("number", newnumber); // target data table | New data | update Condition Statement | update condition parameter list int rows = db. update ("person", values, "name =? ", New String [] {name}); db. close (); return rows ;} /*** delete data based on the specified name ** @ param name * Deletion condition * @ return rows affected rows */public int delete (String name) {SQLiteDatabase db = helper. getWritableDatabase (); // target database table name | Condition Statement | condition parameter int rows = db. delete ("person", "name =? ", New String [] {name}); db. close (); return rows;}/*** query all operations ** @ return query result set */public List
 
  
FindAll () {SQLiteDatabase db = helper. getReadableDatabase (); Cursor cursor = db. query ("person", null, null); // declare the result set // initialize the result set List
  
   
Persons = new ArrayList
   
    
(); // Determine whether results are returned while (cursor. moveToNext () {// declare the result object Person person = null; // retrieve the data of individual data fields in the dataset. // cursor. getColumnIndex ("id"); returns the subscript of the specified field // cursor. getInt (index); or getString (index); is used to obtain the data index of the specified underlying object as the int type; int id = cursor. getInt (cursor. getColumnIndex ("id"); String named = cursor. getString (cursor. getColumnIndex ("name"); String number = cursor. getString (cursor. getColumnIndex ("number"); // instantiate the query result data object person = new Person (id, named, number); // Add data persons to the result set. add (person) ;}// close the connection and release the resource database. close (); // return the result set return persons ;}}
   
  
 

For other code, see the Android blog to add, delete, modify, and query SQLite databases.

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.