Basic operation example of Android SQLite Database

Source: Internet
Author: User
Tags sqlite database
Import android. app. activity; <br/> Import android. content. contentvalues; <br/> Import android. database. cursor; <br/> Import android. database. SQLite. sqlitedatabase; <br/> Import android. graphics. color; <br/> Import android. OS. bundle; <br/> Import android. view. keyevent; <br/> Import android. widget. linearlayout; <br/> Import android. widget. listadapter; <br/> Import android. widget. listview; <br/> Import android. Widget. simplecursoradapter; </P> <p> public class activity01 extends activity {<br/> Private Static int micount = 0; </P> <p>/* database object */<br/> private sqlitedatabase msqlitedatabase = NULL; </P> <p> private final static string database_name = "SQLite. DB "; </P> <p>/* Table name */<br/> private final static string table_name =" Table1 "; </P> <p>/* field in the table */<br/> private final static string table_id = "_ id"; </P> <p> private Final Static string table_num = "num"; </P> <p> private final static string table_data = "data "; </P> <p>/* SQL statement for creating a table */<br/> private final static string create_table = "CREATE TABLE" + table_name <br/> + "(" + table_id + "integer primary key, "+ table_num <br/> +" interger, "+ table_data +" text )"; </P> <p>/* linear Layout */<br/> linearlayout m_linearlayout = NULL; </P> <p>/* List View-display data in the database */<br/> listview m _ Listview = NULL; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); </P> <p>/* Create a linearlayout layout object */<br/> m_linearlayout = new linearlayout (this ); <br/>/* set the properties of the layout linearlayout */<br/> m_linearlayout.setorientation (linearlayout. vertical); <br/> m_linearlayout.setbackgroundcolor (Android. graphics. color. black); </P> <p>/* Create a listview object */<br/> M_L Istview = new listview (this); <br/> linearlayout. layoutparams Param = new linearlayout. layoutparams (<br/> linearlayout. layoutparams. fill_parent, <br/> linearlayout. layoutparams. wrap_content); <br/> m_listview.setbackgroundcolor (color. black); </P> <p>/* Add m_listview to m_linearlayout Layout */<br/> m_linearlayout.addview (m_listview, Param ); </P> <p>/* set to display m_linearlayout Layout */<br/> setcontentview (m_linearlayout); </ P> <p> // open an existing database <br/> msqlitedatabase = This. openorcreatedatabase (database_name, <br/> mode_private, null ); </P> <p> // obtain the database phones cursor <br/> try {<br/>/* Create a table in the database msqlitedatabase */<br/> msqlitedatabase.exe csql (create_table ); <br/>} catch (exception e) {<br/> updataadapter (); <br/>}</P> <p> Public Boolean onkeyup (INT keycode, keyevent event) {<br/> switch (keycode) {<br/> case keyevent. key Code_dpad_left: <br/> adddata (); <br/> break; <br/> case keyevent. keycode_dpad_right: <br/> deletedata (); <br/> break; <br/> case keyevent. keycode_1: <br/> updata (); <br/> break; <br/> case keyevent. keycode_2: <br/> deletetable (); <br/> break; <br/> case keyevent. keycode_3: <br/> deletedatabase (); <br/> break; <br/>}< br/> return true; <br/>}</P> <p>/* delete database */<br/> Public void deletedatabase () {<br/> This. delete Database (database_name); <br/> This. finish (); <br/>}</P> <p>/* delete a table */<br/> Public void deletetable () {<br/> msqlitedatabase.exe csql ("Drop table" + table_name); <br/> This. finish (); <br/>}</P> <p>/* update a data entry */<br/> Public void updata () {<br/> contentvalues CV = new contentvalues (); <br/> cv. put (table_num, micount); <br/> cv. put (table_data, "modified data" + micount); </P> <p>/* update data */<br/> msqlitedatabase. UPDA Te (table_name, CV, <br/> table_num + "=" + integer. tostring (micount-1), null); </P> <p> updataadapter (); <br/>}</P> <p>/* Add a data entry to the table */<br/> Public void adddata () {<br/> contentvalues CV = new contentvalues (); <br/> cv. put (table_num, micount); <br/> cv. put (table_data, "Test Database Data" + micount); <br/>/* insert data */<br/> msqlitedatabase. insert (table_name, null, CV); <br/> micount ++; <br/> updataadapter (); <br/>}</P> <P>/* delete a specified piece of data from the table */<br/> Public void deletedata () {<br/>/* delete data */<br/> msqlitedatabase.exe csql ("delete from" + table_name + "where _ id =" <br/> + integer. tostring (micount); </P> <p> micount --; <br/> If (micount <0) {<br/> micount = 0; <br/>}</P> <p> updataadapter (); <br/>}</P> <p>/* try to display more lines */<br/> Public void updataadapter () {<br/> // obtain the database phones cursor <br/> cursor cur = msqlitedatabase. query (table_n Ame, new string [] {table_id, <br/> table_num, table_data}, null, null); </P> <p> micount = cur. getcount (); <br/> If (cur! = NULL & cur. getcount ()> = 0) {<br/> // listadapter is a bridge between listview and background data <br/> listadapter adapter = new simplecursoradapter (this, <br/> // define the display template for each row in the list <br/> // indicates that each row contains two data items <br/> android. r. layout. simple_list_item_2, <br/> // database cursor object <br/> cur, <br/> // database cursor object <br/> New String [] {table_num, table_data },< br/> // views corresponding to name and number <br/> New int [] {android. r. id. text1, android. r. id. text2}); <br/>/* Add the adapter to m_listview */<br/> m_listview.setadapter (adapter ); <br/>}</P> <p>/* key event processing */<br/> Public Boolean onkeydown (INT keycode, keyevent event) {<br/> If (keycode = keyevent. keycode_back) {<br/>/* close the database (easy to forget) */<br/> msqlitedatabase. close (); <br/> This. finish (); <br/> return true; <br/>}< br/> return Super. onkeydown (keycode, event); <br/>}</P> <p>}

8

The above test data 8 was previously created ..

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.