Android in-depth study of SQLite instances (III)

Source: Internet
Author: User
Tags sqlite database

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

// Query by page 2 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 (string SQL, string [] S) parameter 1 is an SQL statement parameter 2 is the specific values 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 ;}}

Entity operation class

Package EOE. demo; import Java. util. arraylist; import Java. util. list; import android. content. context; import android. database. cursor; import android. database. SQLite. sqlitedatabase; import android. util. log; import it. bean. person; /***** entity operation class * rawquery execution SQL query * execute addition, deletion, and modification SQL * The Inheritance class of sqliteopenhelper mangerdatabase obtains the database management instance * This management instance is obtained by the sqlitedatabase object * This object can execute rawquery and execsql Methods */public class personservic E {private mangerdatabase dbmanger; Public personservice (context) {dbmanger = new mangerdatabase (context) ;}// Save Public void save (person) {/*** open the getwritabledatabase () and getreadabledatabase () Methods of the database to obtain an sqlitedatabase instance used to operate the database. * However, the getwritabledatabase () method opens the database in read/write mode. Once the disk space of the database is full, the database can only read but cannot write. * If getwritabledatabase () is used () an error occurs. * The getreadabledatabase () method first opens the database in read/write mode. If the disk space of the database is full, the database fails to be opened. * If the opening fails, the database will continue to be opened in read-only mode. * Sqlitedatabase SQLite database management class */sqlitedatabase database = dbmanger.getwritabledatabase(registry.database.exe csql ("insert into person (name, age) values (?,?) ", New object [] {person. getname (), person. getage ()});} // update public void Update (person) {sqlitedatabase database = dbmanger. getwritabledatabase (); // execsqlis the execution of SQL statement database.exe csql ("Update person set name = ?, Age =? Where personid =? ", New object [] {person. getname (), person. getage (), person. getpersonid ()});} // query data by ID: public person findbyid (integer ID) {sqlitedatabase database = dbmanger. getwritabledatabase (); // cursor is a cursor type. cursor is actually a data set in the database. cursor = database. rawquery ("select * From person where personid =? ", New string [] {string. valueof (ID)}); If (cursor. movetonext () {log. I ("XXX", "XXX" + String. valueof (cursor. getint (3); person = new person (cursor. getint (0), cursor. getstring (1), cursor. getshort (2); Return person;} return NULL;} // Delete public void Delete (integer... IDS) {If (IDs. length> 0) {stringbuilder sb = new stringbuilder (); For (integer ID: IDS) {sb. append ('? '). Append (',');}

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.