Android in-depth study of SQLite instances (IV)

Source: Internet
Author: User

Written in front of, this article Reprinted from http://www.eoeandroid.com/thread-81914-1-1.html

// Delete the last character sb. deletecharat (sb. length ()-1); sqlitedatabase database = dbmanger. getwritabledatabase (); // execsqlis the execution of SQL statement database.exe csql ("delete from person where personid in (" + Sb + ")", (object []) IDs );}} // query by page a public list getdateperson (INT startresult, int maxresult) {list persons = new arraylist (); sqlitedatabase database = dbmanger. getwritabledatabase (); // cursor is a cursor type. A cursor is actually a dataset in the database. // rawquery (string SQL, strin G [] S) parameter 1 is an SQL statement parameter 2 is a specific value stored in the condition placeholder in parameter 1 SQL statement. These values are a string array cursor = database. rawquery ("select * From person limit ?,? ", New string [] {string. valueof (startresult), String. valueof (maxresult)}); While (cursor. movetonext () {persons. add (new person (cursor. getint (0), cursor. getstring (1), cursor. getshort (2);} return persons;} // two public cursor getdaterawperson (INT startresult, int maxresult) {// list persons = new arraylist (); sqlitedatabase database = dbmanger. getwritabledatabase (); // cursor is a cursor type. A cursor is actually a dataset in the database. // rawquery (stri Ng SQL, string [] S) parameter 1 is an SQL statement parameter 2 is a specific value stored in the condition placeholder in parameter 1 SQL statement. These values are a string array return database. rawquery ("select personid AS _ id, name, age from person limit ?,? ", New string [] {string. valueof (startresult), String. valueof (maxresult)});} // obtain the total number of records public long getcount () {sqlitedatabase database = dbmanger. getwritabledatabase (); // cursor is a cursor type. cursor is actually a data set in the database. cursor = database. rawquery ("select count (*) from person", null); If (cursor. movetolast () {return cursor. getlong (0) ;}return 0 ;}}

Business type 2

Package EOE. demo; import Java. util. arraylist; import Java. util. list; import it. bean. person; import android. content. contentvalues; import android. content. context; import android. database. cursor; import android. database. SQLite. sqlitedatabase; /*** this class does not require addition, deletion, query, modification, and modification operations based on SQL statements * But the sqlitedatabase object is operated by constructing an SQL statement internally **/public class personsqlservice {private mangerdatabase dbmanger; public personsqlservice (context contex T) {dbmanger = new mangerdatabase (context);} public void save (person) {sqlitedatabase database = dbmanger. getwritabledatabase (); contentvalues values = new contentvalues (); values. put ("name", person. getname (); values. put ("Age", person. getage (); // name of the parameter table to construct the correct field ing database for the insert statement. insert ("person", "name", values);} public void Update (person) {sqlitedatabase database = dbmanger. getwritabledat Abase (); contentvalues values = new contentvalues (); values. put ("name", person. getname (); values. put ("Age", person. getage (); // parameter table name update ing relationship condition placeholder value database. update ("person", values, "personid =? ", New string [] {string. valueof (person. getpersonid ()});} public person find (integer ID) {sqlitedatabase database = dbmanger. getwritabledatabase (); // execute the query parameter. The fields returned by the table name specify the condition and specify whether the condition values are grouped or not. query ("person", new string [] {"personid", "name", "Age"}, "personid =? ", New string [] {string. valueof (ID)}, null); If (cursor. movetonext () {return new person (cursor. getint (0), cursor. getstring (1), cursor. getshort (2);} return NULL ;}
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.