Android database Android comes with database SQLite operation step by step diagram tutorial

Source: Internet
Author: User

The use of Android in the development process of the database, not only better security, but also the effect of data access is better.

Attached source code

http://download.csdn.net/detail/wyx100/8670843

Development planning Main Interface

Complete input of data, trigger save

Post-boot interface

Input data

Data Display Interface

The database is displayed after reading the data

Deleting records

Development Sequence Main interface

Class Code

Package Com.example.sqlitetest;import Android.os.bundle;import Android.app.activity;import Android.content.contentvalues;import Android.content.intent;import Android.view.view;import Android.widget.Button Import Android.widget.edittext;public class Activitymain_1 extends Activity {//Declaration EditText Instance Private EditText Et1,et2, et3;//declaring a Button instance private button button; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO Auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (r.layout.main_1);//Get instance Object Et1 = ( EditText) Findviewbyid (r.id.edittext01) Et2 = (EditText) Findviewbyid (r.id.edittext02); et3 = (EditText) Findviewbyid ( r.id.edittext03), Button = (button) Findviewbyid (R.id.button);//Event Button.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {String name=et1.gettext (). toString (); String URL =et2.gettext (). toString (); String desc = Et3.gettext (). toString (); Contentvalues values = new Contentvalues (); Values.put ("name", name); valueS.put ("url", url); Values.put ("desc", desc);//Instantiate Database helper class DBHelper helper = new DBHelper (Getapplicationcontext ());// Open database Helper.open (); Insert data Helper.insert (values);//Instantiate intentintent intent = new Intent (Activitymain_1.this,queryactivity.class); StartActivity (intent); Helper.close ();}});}}

Interface layout
<?xml version= "1.0" encoding= "UTF-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " ><textviewandroid:id= "@+id/textview01" android:layout_width= "fill_parent" android:layout_height= "Wrap_ Content "android:text=" site name "/><edittextandroid:id=" @+id/edittext01 "android:layout_width=" Fill_parent " android:layout_height= "Wrap_content"/><textviewandroid:id= "@+id/textview02" android:layout_width= "Fill_ Parent "android:layout_height=" wrap_content "android:text=" URL "/><edittextandroid:id=" @+id/EditText02 " Android:layout_width= "Fill_parent" android:layout_height= "wrap_content"/><textviewandroid:id= "@+id/ TextView03 "android:layout_width=" fill_parent "android:layout_height=" wrap_content "android:text=" site description "/>< Edittextandroid:id= "@+id/edittext03" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android:height= "100px"/><buttonAndroid:id= "@+id/button" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= " Add the/></linearlayout>

2. Data Display Interface

Class Code

Package Com.example.sqlitetest;import Android.app.alertdialog;import Android.app.listactivity;import Android.content.dialoginterface;import Android.database.cursor;import Android.os.bundle;import Android.view.View; Import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.listview;import Android.widget.simplecursoradapter;public class Queryactivity extends ListActivity { Private ListView listview;private Cursor C; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate ( Savedinstancestate);//setcontentview (r.layout.row);//Instantiate Database Helper class final DBHelper help = new DBHelper (this); Help.open (); C= Help.query (); Query ();//Hint Dialog final Alertdialog.builder Builder = new Alertdialog.builder (this); Listview.setonitemclicklistener ( New Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<?> parent, View view,int position, long ID {Final Long temp = Id;builder.setmessage ("Do you really want to delete this record?"). ").Setpositivebutton ("Yes", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (Dialoginterface dialog, int which) {System.out.println ("yes"), Help.del ((int) temp);//Requery//query (); Cursor C = help.query ();//list item array string[] from ={"_id", "name", "url", "desc"};//list item idint[] to ={R.ID.TEXT0,R.ID.TEXT1, r.id.text2,r.id.text3};//Adapter Simplecursoradapter adapter = new Simplecursoradapter (Getapplicationcontext (), R.LAYOUT.ROW,C,FROM,TO); ListView ListView = Getlistview (); Listview.setadapter (adapter);}}). Setnegativebutton ("No", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (Dialoginterface dialog, int which) {//TODO auto-generated Method stub}}); Builder.create (); Builder.show ();});} @Overrideprotected void OnStop () {//TODO auto-generated method Stubsuper.onstop ();} public void query () {//list item array string[] from ={"_id", "name", "url", "desc"};//list item idint[] to ={R.ID.TEXT0,R.ID.TEXT1, r.id.text2,r.id.text3};//Adapter Simplecursoradapter adapter = new Simplecursoradapter (THIS,R.LAYOUT.ROW,C,FROm,to);//adapter.notifydatasetchanged ();//List View ListView = Getlistview ();//Add Adapter Listview.setadapter for List view (adapter);}} 


Interface layout

<?xml version= "1.0" encoding= "UTF-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" horizontal "android:layout_width=" Fill_parent "android:layout_height=" Fill_parent " Android:layout_gravity= "center_vertical" ><textviewandroid:id= "@+id/text0" android:layout_width= "Wrap_ Content "android:layout_height=" wrap_content "android:paddingright=" 10px "/><textviewandroid:id=" @+id/text1 "Android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:paddingright=" 10px "/>< Textviewandroid:id= "@+id/text2" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android: paddingright= "10px"/><textviewandroid:id= "@+id/text3" android:layout_width= "Wrap_content" Android:layout_ height= "Wrap_content" android:paddingright= "10px"/></linearlayout>
Database processing Classes
Package Com.example.sqlitetest;import Android.content.contentvalues;import Android.content.context;import Android.database.cursor;import Android.database.sqlite.sqlitedatabase;import Android.database.sqlite.sqliteopenhelper;public class DBHelper extends Sqliteopenhelper {private static final String DB _name= "coll.db";//???? private static final String tbl_name= "logtable";//???? Sql??? private static final String create_tbl = "CREATE table logtable" + "(_id integer primary key autoincrement,name Text,url t Ext,desc text) ";//sqlitedatabase??? Private Sqlitedatabase Db;public DBHelper (context context) {Super (context, db_name, NULL, 2);} @Overridepublic void OnCreate (Sqlitedatabase db) {db.execsql (CREATE_TBL);} //?????? public void Open () {db = Getwritabledatabase ();} @Overridepublic void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {//TODO auto-generated method stub}//?? ???? public void Insert (Contentvalues values) {Db.insert (tbl_name, null, values);} //??????? Public Cursor query () {CurSor C = db.query (tbl_name, NULL, NULL, NULL, NULL, NULL, NULL); return c;} //??? public void del (int id) {Db.delete (Tbl_name, "_id=?", new String[]{string.valueof (ID)}); //??????? public void Close () {if (db! = null) {Db.close ();}}}

Startup configuration file

Androidmanifest.xml

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/        Android "package=" Com.example.sqlitetest "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/> <application android:allowbackup=" "True" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/ Apptheme "> <activity android:name=" com.example.sqlitetest.ActivityMain_1 "Android:labe L= "@string/app_name" > <intent-filter> <action android:name= "android.intent.action.m AIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filter > </activity> <activity android:name= "Com.example.sqlitetest.QueryActivity" ></a Ctivity> </application></manifest> 



Android database Android comes with database SQLite operation step by step diagram tutorial

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.