Android in-depth study of SQLite instances (II)

Source: Internet
Author: User

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

Package it. service; 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 personserv Ice {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 (',');} // 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, 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 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 ;}

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.