Android SQLite database application (1)

Source: Internet
Author: User

Let's start with a small program. The detailed parsing introduction will be detailed in later articles. It's already am. I am so sleepy that I can only move this job to tomorrow. Paste the code first today.

(Store data in the database, and then read the data)

Main. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <textview <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: id = "@ + ID/textview" <br/> </linearlayout> <br/>

Activity Code

Package cn.com. SQLite; </P> <p> Import Java. util. arraylist; </P> <p> Import android. app. activity; <br/> Import android. content. contentvalues; <br/> Import android. content. context; <br/> Import android. database. cursor; <br/> Import android. database. SQLite. sqlitedatabase; <br/> Import android. database. SQLite. sqliteopenhelper; <br/> Import android. database. SQLite. sqlitedatabase. cursorfactory; <br/> Import android. OS. bundle; <br/> Import android. util. log; <br/> Import android. widget. textview; </P> <p>/** <br/> * @ author chenzheng_java <br/> * @ description application of SQLite database in Android <br/> * @ since 2011/03/05 <br/> */<br/> public class sqliteactivity extends activity {<br/> private string result = "result: /n "; <br/> private string tablename =" chenzheng_java "; <br/> Public static final int version_1 = 1; </P> <P> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> Run (); <br/> textview = (textview) findviewbyid (R. id. textview); <br/> textview. settext (result ); <br/>}</P> <p>/** <br/> * @ Description: add, delete, modify, and query data <br/> */<br/> private void run () {<br/> clear (); <br/> Save (); <br/> Read (); <br/>}</P> <p>/** <br/> * Read records from data tables <br/> */<br/> private void read () {<br/> mysqliteopenhandler handler = new mysqliteopenhandler (this, tablename, <br/> null, version_1); <br/> sqlitedatabase database = handler. getwritabledatabase (); <br/> cursor = database. query (tablename, null, <br/> null); <br/> int COUNT = cursor. getcount (); <br/> log. I ("notification", "Total number of records" + count); </P> <p> int Index = cursor. getcolumn Index ("name"); <br/> log. I ("notification", "nameindex" + index); <br/> int indexofage = cursor. getcolumnindex ("Age"); </P> <p> cursor. movetofirst (); <br/> while (! Cursor. isafterlast () {<br/> log. I ("notification", "here"); <br/> string name = cursor. getstring (INDEX); <br/> result + = "/n name:" + name; <br/> int age = cursor. getint (indexofage); <br/> result + = "Age:" + age; <br/> cursor. movetonext (); <br/>}< br/> database. close (); <br/>}</P> <p>/** <br/> * Save some data to the table <br/> */<br/> private void save () {<br/> arraylist <beauty> beautylist = getdata (); <br/> mysqliteopenhandler handl ER = new mysqliteopenhandler (<br/> sqliteactivity. this, tablename, null, version_1); <br/> sqlitedatabase database = handler. getwritabledatabase (); <br/> for (Beauty: beautylist) {<br/> // start transaction <br/> database. begintransaction (); <br/> // objects that store column names and values in the form of key-value pairs <br/> contentvalues = new contentvalues (); <br/> contentvalues. put ("name", beauty. getname (); <br/> contentvalues. put ("Age", beauty. Getage (); <br/> long flag = database. insertorthrow (tablename, "Age", contentvalues); <br/> log. I ("notification", beauty. tostring (); <br/> If (flag =-1) {<br/> log. I ("notification", "insert operation failed"); <br/>} else {<br/> database. settransactionsuccessful (); <br/>}< br/> // end transaction <br/> database. endtransaction (); <br/>}</P> <p> database. close (); </P> <p >}</P> <p>/** <br/> * clear original records in the table <br/> */<br/> private void clear () {<br/> MySQL Iteopenhandler handler = new mysqliteopenhandler (<br/> sqliteactivity. this, tablename, null, version_1); <br/> sqlitedatabase database = handler. getwritabledatabase (); <br/> database. delete (tablename, null, null); <br/> database. close (); </P> <p >}</P> <p>/** <br/> * @ return initialize data <br/> */<br /> private arraylist <beauty> getdata () {<br/> arraylist <beauty> beautylist = new arraylist <beauty> (); <br/> Beauty = NULL; </P> <p> beauty = new beauty ("Xiao Yi Xian", 23); <br/> beautylist. add (beauty); </P> <p> beauty = new beauty ("Xiao xuner", 21); <br/> beautylist. add (beauty); </P> <p> beauty = new beauty ("Queen of doumeisha", 24); <br/> beautylist. add (beauty); </P> <p> return beautylist; </P> <p >}</P> <p>/** <br/> * @ author chenzheng_java <br/> * @ Description: Create your own SQLite. and update the help class <br/> * @ since 2011/03/05 <br/> */<br/> private class my Sqliteopenhandler extends sqliteopenhelper {</P> <p> Public mysqliteopenhandler (context, string name, <br/> cursorfactory factory, int version) {<br/> super (context, name, factory, version); <br/> log. I ("notification", "mysqliteopenhandler instantiated "); <br/>}</P> <p>/** <br/> * The oncreate method is executed when the user-queried table does not exist in the system, here we can implement this method and perform some additional Operations <br/> */<br/> @ override <br/> Public void oncreate (sqlitedatabase dB) {<br/> db.exe CS QL ("Drop table if exists" + tablename); <br/> dB <br/>. execsql ("create table if not exists '" <br/> + tablename <br/> + "' (ID integer primary key, name varchar (20), age integer) "); </P> <p> log. I ("notification", "table created successfully! "); <Br/>}</P> <p>/** <br/> * run the onupgrade method when the application version in the system changes. <br/> */<br /> @ override <br/> Public void onupgrade (sqlitedatabase dB, int oldversion, int newversion) {<br/> log. I ("notice", "version upgrade successful! "); </P> <p >}</P> <p>/** <br/> * @ author chenzheng_java <br/> * @ description beauty entity class <br/> */<br/> private class beauty {<br/> string name; <br/> int age; </P> <p> Public beauty () {<br/>}</P> <p> Public beauty (string name, int age) {<br/> This. name = Name; <br/> This. age = age; <br/>}</P> <p> Public String getname () {<br/> return name; <br/>}</P> <p> Public void setname (string name) {<br/> This. name = Name; <br/>}</P> <p> Public int getage () {<br/> return age; <br/>}</P> <p> Public void setage (INT age) {<br/> This. age = age; <br/>}</P> <p> @ override <br/> Public String tostring () {<br/> return "Beauty [age =" + age + ", name =" + name + "]"; <br/>}</P> <p>}

Others are default. Run the command to obtain the preceding result.

 

------------------------------------------------------------------------

Note that in the constructor of the mysqliteopenhandler class, the second parameter actually represents the database name rather than the table name. The table name is directly used for construction, it is to make the Code look a little less and satisfy the author's desire to be lazy. Remember

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.