Android development step-by-step instance 4-database access example

Source: Internet
Author: User
Tags constant definition response code sqlite database

In this example, we will focus on using the SQLite database access method provided by the Android platform, including read/write access.
1. Follow the following configuration and the method described in step-by-step instance 1 to create the base of the entire project:
Project name: examplefour
Platform: android2.0;
Application name: examplefour
Package name: COM. Example
Activity: mainactivity
Resource file: Main. xml
The attributes of a textview are as follows:
Textview
ID: @ + ID/contenttextview
Text: content:
An editview:
ID: @ + ID/editcontent
Text: blank
Layout width: fill_parent
Two buttons:
ID: @ + ID/insertbutton
Text: insert
Layout width: fill_parent
ID: @ + ID/readbutton
Text: Read
Layout width: fill_parent

2. Add the following code to the sqliteopenhelper class in mainactivity ):
Public class exampledbhelper extends sqliteopenhelper {

Exampledbhelper (context ){
Super (context, db_name, null, db_version );
}

@ Override
Public void oncreate (sqlitedatabase arg0 ){
Stringbuffer sb = new stringbuffer ();
SB. append ("create table"). append (table_name );
SB. append ("("). append (column_sn). append ("int not null ,");
SB. append (column_content). append ("text not null );");
Arg0.execsql (sb. tostring ());
}

@ Override
Public void onupgrade (sqlitedatabase arg0, int arg1, int arg2 ){
}

}

3. Add the following constant definition to mainactivity:
Public static final int db_version = 1;
Public static final string db_name = "example_content ";
Public static final string table_name = "et_content ";
Public static final string column_sn = "Sn ";
Public static final string column_content = "content ";

4. Add the following variable definitions to mainactivity:
Private exampledbhelper dbhelper = NULL;

5. Write the event response code for the button:
Private void connect_control_events (){
Button buttoninsert = (button) findviewbyid (R. Id. insertbutton );
Buttoninsert. setonclicklistener (buttoninsert_listener );

Button buttonread = (button) findviewbyid (R. Id. readbutton );
Buttonread. setonclicklistener (buttonread_listener );
}

Private button. onclicklistener buttoninsert_listener = new button. onclicklistener (){
Public void onclick (view v ){
Sqlitedatabase DB = dbhelper. getwritabledatabase ();

Edittext contentcontrol = (edittext) findviewbyid (R. Id. contentedittext );
String strcontent = contentcontrol. geteditabletext (). tostring ();

String [] Columns = new string [2];
Columns [0] = column_sn;
Columns [1] = column_content;
Cursor cur = dB. Query (table_name, columns, null );
If (cur. getcount ()> 0 ){
Stringbuffer sb = new stringbuffer ();
SB. append ("Update"). append (table_name). append ("set"). append (column_content );
SB. append ("="). append ("'"). append (strcontent). append ("'");
DB. begintransaction ();
Try {
Db.exe csql (sb. tostring ());
DB. settransactionsuccessful ();
} Finally {
DB. endtransaction ();
}
} Else {
Stringbuffer sb = new stringbuffer ();
SB. append ("insert into"). append (table_name). append ("("). append (column_sn );
SB. append (","). append (column_content). append (")");
SB. append ("values (1, '"). append (strcontent). append ("')");
DB. begintransaction ();
Try {
Db.exe csql (sb. tostring ());
DB. settransactionsuccessful ();
} Finally {
DB. endtransaction ();
}
Cur. Close ();
}
}
};
 
Private button. onclicklistener buttonread_listener = new button. onclicklistener (){
Public void onclick (view v ){
Sqlitedatabase DB = dbhelper. getreadabledatabase ();
String [] Columns = new string [2];
Columns [0] = column_sn;
Columns [1] = column_content;
Cursor cur = dB. Query (table_name, columns, null );
If (cur. getcount ()> 0 ){
Cur. movetofirst ();
String strcontent = cur. getstring (1 );
Edittext contentcontrol = (edittext) findviewbyid (R. Id. contentedittext );
Contentcontrol. settext (strcontent );
}
}
};

6. Add the following associated code to the end of the oncreate function of mainactivity:
Dbhelper = new exampledbhelper (this. getbasecontext ());
Connect_control_events ();

7. Test: first enter some data randomly in edittext, click Insert to insert the data, then stop the program, restart the program, and then click read to find that edittext is the previous data input;

8. improvement: considering this program is an example, we put the sqliteopenhelper overload in the form of internal class in mainactivity for implementation. If in a project, we recommend that you separate this class to form xxxhelper, then, the sqliteopenhelper overload is implemented in this class (which is implemented in the form of internal classes). In this way, all data access code can be implemented in xxxhelper to unify functions and modules.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/jackxinxu2100/archive/2010/01/27/5260909.aspx

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.