Android Learning (10) SQLite operation based on built-in functions

Source: Internet
Author: User

Main.xml

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context=". Mainactivity " >    <ButtonAndroid:id= "@+id/button1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Insert Data" />    <ButtonAndroid:id= "@+id/button2"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Read Data" />    <ButtonAndroid:id= "@+id/button3"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Modify Data" />    <ButtonAndroid:id= "@+id/button4"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Delete Data" /></LinearLayout>

Main.java

 PackageCom.example.sqlitedemo2;Importandroid.app.Activity;Importandroid.content.ContentValues;ImportAndroid.database.Cursor;Importandroid.database.sqlite.SQLiteDatabase;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {Button btn1;    Button btn2;    Button Btn3;    Button Btn4;    Sqlitedatabase DB; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); DB= Openorcreatedatabase ("Stu.db", Mode_private,NULL); Db.execsql ("CREATE table if not exists Tb_user (ID integer primary key autoincrement,name text not null,age integer not null,sex text NOT null) "); BTN1=(Button) Findviewbyid (R.id.button1); BTN2=(Button) Findviewbyid (R.id.button2); Btn3=(Button) Findviewbyid (R.id.button3); Btn4=(Button) Findviewbyid (R.ID.BUTTON4); Btn1.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {contentvalues values=Newcontentvalues (); Values.put ("Name", "John Doe"); Values.put ("Age", 20); Values.put ("Sex", "female"); Db.insert ("Tb_user",NULL, values);                                Values.clear (); Values.put ("Name", "Harry"); Values.put ("Age", 22); Values.put ("Sex", "male"); Db.insert ("Tb_user",NULL, values); Toast.maketext (mainactivity. This, "Add Success", 1). Show ();        }        }); Btn2.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Cursor cur= Db.query ("Tb_user",Newstring[]{"Name", "Age", "Sex"},NULL,NULL,NULL,NULL,NULL);  while(Cur.movetonext ()) {String name= Cur.getstring (Cur.getcolumnindex ("name")); intAge = Cur.getint (The Cur.getcolumnindex ("Age")); String Sex= Cur.getstring (Cur.getcolumnindex ("Sex"))); LOG.I ("Stuinfo", Name + "," + Age + "," +sex);            } cur.close ();                        }                    }); Btn3.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//Modifying DataContentvalues values =Newcontentvalues (); Values.put ("Name", "Zhang San Fung"); intresult = Db.update ("Tb_user", Values, "id=?",Newstring[]{"1"}); if(Result > 0) Toast.maketext (mainactivity. This, "modified successfully", 1). Show (); ElseToast.maketext (mainactivity. This, "Modification failed", 1). Show ();                }        }); Btn4.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//Delete Data                intresult = Db.delete ("Tb_user", "id=?",Newstring[]{"1"}); if(Result > 0) Toast.maketext (mainactivity. This, "Delete succeeded", 1). Show (); ElseToast.maketext (mainactivity. This, "Delete Failed", 1). Show ();    }        }); }}

Android Learning (10) SQLite operation based on built-in functions

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.