Android improves SQLite paging reading in Article 8

Source: Internet
Author: User
Tags sqlite database

This article from http://blog.csdn.net/hellogv/, reference must indicate the source!

Android contains SQLite, which is commonly used in embedded systems, saving developers from porting and installing it themselves. SQLite supports most sql92 standards, and many commonly used SQL commands can be used on SQLite. In addition, Android also provides a series of custom methods to simplify operations on the SQLite database. However, it is recommended that standard SQL statements be used for programs with cross-platform requirements. After all, it is easy to port between multiple platforms.

First paste the results of the program running in this article:

This article describes the basic usage of SQLite, such as creating a database, using SQL commands to query data tables, inserting data, and disabling a database, and use the gridview to implement a paging column (about the usage of the gridview), used to display data by page.

The source code of pagebuttons. xml on the page bar is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_height = "wrap_content" Android: paddingbottom = "4dip" <br/> Android: layout_width = "fill_parent"> <br/> <textview Android: layout_width = "wrap_content" <br/> Android: layout_below = "@ + ID/itemimage" Android: layout_height = "wrap_content" <br/> Android: text = "textview01" Android: layout_centerhorizontal = "true" <br/> Android: id = "@ + ID/itemtext"> <br/> </textview> <br/> </relativelayout>

The source code of Main. XML is as follows:

 

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> <button Android: layout_height = "wrap_content" <br/> Android: layout_width = "fill_parent" Android: id = "@ + ID/btncreatedb" <br/> Android: text = "create database"> </button> <br/> <button Android: layout_height = "wrap_content" <br/> Android: layout_width = "fill_parent" Android: text = "insert a series of experiment data" Android: id = "@ + ID/btninsertrec"> </button> <br/> <button Android: layout_height = "wrap_content" Android: id = "@ + ID/btnclose" <br/> Android: text = "Close Database" Android: layout_width = "fill_parent"> </button> <br/> <edittext Android: text = "@ + ID/edittext01" Android: id = "@ + ID/edittext01" <br/> Android: layout_width = "fill_parent" Android: layout_height = "256dip"> </edittext> <br/> <gridview Android: id = "@ + ID/gridview" Android: layout_width = "fill_parent" <br/> Android: layout_height = "32dip" Android: numcolumns = "auto_fit" <br/> Android: columnwidth = "40dip"> </gridview> <br/> </linearlayout> <br/>

The program source code in this article is as follows:

Package COM. testsqlite; </P> <p> Import Java. util. arraylist; <br/> Import Java. util. hashmap; <br/> Import android. app. activity; <br/> Import android. database. cursor; <br/> Import android. database. sqlexception; <br/> Import android. database. SQLite. sqlitedatabase; <br/> Import android. OS. bundle; <br/> Import android. util. log; <br/> Import android. view. view; <br/> Import android. widget. adapterview; <br/> import Ndroid. widget. adapterview. onitemclicklistener; <br/> Import android. widget. button; <br/> Import android. widget. edittext; <br/> Import android. widget. gridview; <br/> Import android. widget. simpleadapter; </P> <p> public class testsqlite extends activity {<br/>/** called when the activity is first created. */<br/> button btncreatedb, btninsert, btnclose; <br/> edittext edtsql; // display paging data <br/> sqlitedataba Se dB; <br/> int ID; // The ID accumulation mark when the record is added, must be global <br/> static final int pagesize = 10; // when paging, total data per page <br/> Private Static final string table_name = "Stu"; <br/> Private Static final string id = "ID "; <br/> Private Static final string name = "name"; </P> <p> simpleadapter sapageid; // pagination bar adapter <br/> arraylist <pashmap <string, string> lstpageid; // data source in the paging column, which is related to pagesize and total data count </P> <p> @ override <br/> Public void oncreat E (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> btncreatedb = (button) This. findviewbyid (R. id. btncreatedb); <br/> btncreatedb. setonclicklistener (New clickevent (); </P> <p> btninsert = (button) This. findviewbyid (R. id. btninsertrec); <br/> btninsert. setonclicklistener (New clickevent (); </P> <p> btnclose = (button) This. findviewbyid (R. Id. btnclose); <br/> btnclose. setonclicklistener (New clickevent (); </P> <p> edtsql = (edittext) This. findviewbyid (R. id. edittext01); </P> <p> gridview = (gridview) findviewbyid (R. id. gridview); // control of the paging bar <br/> // generate a dynamic array and transfer data <br/> lstpageid = new arraylist <pashmap <string, string> (); </P> <p> // generate the imageitem of the adapter <===> dynamic array elements, one-to-one correspondence between the two <br/> sapageid = new simpleadapter (testsqlite. this, // No explanation <br /> Lstpageid, // data source <br/> r. layout. pagebuttons, // XML implementation <br/> New String [] {"itemtext"}, <br/> New int [] {R. id. itemtext}); </P> <p> // Add and display <br/> gridview. setadapter (sapageid); <br/> // Add Message Processing <br/> gridview. setonitemclicklistener (New onitemclicklistener () {</P> <p> @ override <br/> Public void onitemclick (adapterview <?> Arg0, view arg1, int arg2, <br/> long arg3) {<br/> loadpage (arg2 ); // read the corresponding data based on the selected page <br/>}< br/> }); </P> <p >}</P> <p> class clickevent implements view. onclicklistener {</P> <p> @ override <br/> Public void onclick (view v) {<br/> If (V = btncreatedb) {<br/> createdb (); <br/>} else if (V = btninsert) {<br/> insertrecord (16 ); // insert 16 records <br/> refreshpage (); <br/>} else if (V = btnclose) {<br/> dB. close (); <br/>}</P> <p>/* <br/> * reads the paging data of a specified ID. <br /> * SQL: select * From table_name limit 9 offset 10; <br/> * Indicates retrieving data from the table_name table and skipping 10 rows, fetch 9 rows <br/> */<br/> void loadpage (INT pageid) <br/> {<br/> string SQL = "select * from" + table_name + <br/> "Limit" + String. valueof (pagesize) + "offset" + String. valueof (pageid * pagesize); <br/> cursor rec = dB. rawquery (SQL, null); </P> <p> settitle ("total data on the current page:" + String. valueof (Rec. getcount (); </P> <p> // obtain the field name <br/> String title = ""; <br/> int colcount = rec. getcolumncount (); <br/> for (INT I = 0; I <colcount; I ++) <br/> Title = title + rec. getcolumnname (I) + ""; </P> <p> // list all data <br/> string content = ""; <br/> int reccount = rec. getcount (); <br/> for (INT I = 0; I <reccount; I ++) {// locate a data record <br/> rec. movetoposition (I); <br/> for (INT II = 0; II <colcount; II ++) // locate each field in a data record <br/>{< br/> content = content + rec. getstring (ii) + ""; <br/>}< br/> content = content + "/R/N "; <br/>}</P> <p> edtsql. settext (Title + "/R/N" + content); // displayed <br/> rec. close (); <br/>}</P> <p>/* <br/> * Create a database and data table in the memory <br/> */<br/> void createdb () {<br/> // create a database in the memory <br/> DB = sqlitedatabase. create (null); <br/> log. E ("DB path", DB. getpath (); <br/> string amount = string. valueof (databaselist (). length); <br/> log. E ("DB amount", amount ); <br/> // create a data table <br/> string SQL = "CREATE TABLE" + table_name + "(" + id <br/> + "text not null, "+ name +" text not null "+"); "; <br/> try {<br/> db.exe csql (" Drop table if exists "+ table_name ); <br/> db.exe csql (SQL); <br/>} catch (sqlexception E) {}< br/>}</P> <p>/* <br/> * Insert n data records <br/> */<br/> void insertrecord (INT n) {<br/> int Total = ID + N; <br/> for (; id <total; Id ++) {<br/> string SQL = "insert into" + table_name + "(" + ID + "," + name <br/> + ") values ('" + String. valueof (ID) + "', 'test');"; <br/> try {<br/> db.exe csql (SQL ); <br/>} catch (sqlexception E) {<br/>}</P> <p>/* <br/> * refresh the page after insertion <br/> */ <br/> void refreshpage () <br/> {<br/> string SQL = "select count (*) from" + table_name; <br/> cursor rec = dB. rawquery (SQL, null); <br/> rec. movetolast (); <br/> long recsize = rec. getlong (0); // get the total number <br/> rec. close (); <br/> int pagenum = (INT) (recsize/pagesize) + 1; // obtain the number of pages </P> <p> lstpageid. clear (); <br/> for (INT I = 0; I <pagenum; I ++) {<br/> hashmap <string, string> map = new hashmap <string, string> (); <br/> map. put ("itemtext", "no. "+ String. valueof (I); </P> <p> lstpageid. add (MAP); <br/>}< br/> sapageid. notifydatasetchanged (); <br/>}< br/>}

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.