Android SQLite database Simple to use

Source: Internet
Author: User
Tags sqlite sqlite database stub create database



First, the principle



SQLite related Introduction I do not say, want to understand the classmate can Google or Baidu, good ~ directly into the theme, to use SQLite storage data, first create a database, create the following methods:




Sqlitedatabase db = openorcreatedatabase ("database name. db", context.mode_private, NULL);
Db.execsql ("DROP TABLE IF EXISTS person");
Create a person table
Db.execsql ("Create TABLE person (_id INTEGER PRIMARY KEY autoincrement, name VARCHAR, age SMALLINT)" When the database is created you can use SQLite , then we have to increase the database, delete, change, check operation L.



Increase:


The code is as follows

person who = new person ();
Person.setname ("John");
Person.setage (19);
Inserting data
Db.execsql ("INSERT into person VALUES (NULL,?,?)", new object[] {person.getname (), Person.getage ()});


By deleting:





The code is as follows
String whereclause = "_id=?";/ /delete Condition
String[] Whereargs = {"1"};//deleted condition parameters
Db.delete ("person", Whereclause,whereargs);


To perform a delete change:





The code is as follows

Person Person2 = new person ();
Person2.setname ("John");
Person2.setage (20);
Contentvalues CV = new Contentvalues ();
Cv.put ("Age", Person2.getage ()); Modify Content
Db.update ("Person", CV, "name =?", new string[] {person2.getname ()});


Check:


The code is as follows

Cursor C = Db.rawquery ("select * from person where name=?", New string[]{"John"});
while (C.movetonext ()) {
int ID =integer.parseint (c.getstring (C.getcolumnindex ("_id"));
String name =c.getstring (C.getcolumnindex ("name"));
String age = c.getstring (C.getcolumnindex (' age '));
LOG.V ("Test", "ID:" + ID + "Name:" + name + "Age:" + age);
}


Here is the complete code.






Second, the Code



First step:


The code is as follows

<relativelayout xmlns: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" >








<button


Android:id= "@+id/btn_create_database"


Android:layout_width= "Fill_parent"


android:layout_height= "Wrap_content"


android:text= "CreateDatabase"


/>





<button


Android:id= "@+id/btn_add"


Android:layout_width= "Fill_parent"


android:layout_height= "Wrap_content"


android:layout_below= "@id/btn_create_database"


android:text= "Add"


/>





<button


Android:id= "@+id/btn_update"


Android:layout_width= "Fill_parent"


android:layout_height= "Wrap_content"


android:layout_below= "@id/btn_add"


android:text= "Update"


/>


<button


Android:id= "@+id/btn_query"


Android:layout_width= "Fill_parent"


android:layout_height= "Wrap_content"


android:layout_below= "@id/btn_update"


android:text= "Query"


/>


<button


Android:id= "@+id/btn_delete"


Android:layout_width= "Fill_parent"


android:layout_height= "Wrap_content"


android:layout_below= "@id/btn_query"


android:text= "Delete"


/>





</RelativeLayout>



Step Two:





The code is as follows




Package com.msquirrel.activity;





Import android.app.Activity;


Import android.content.ContentValues;


Import Android.content.Context;


Import Android.database.Cursor;


Import Android.database.sqlite.SQLiteDatabase;


Import Android.os.Bundle;


Import Android.util.Log;


Import Android.view.View;


Import Android.view.View.OnClickListener;


Import Android.widget.Button;





Import Com.example.demo_sqlite. R


Import Com.msquirrel.model.Person;





/**


* Execute CREATE DATABASE, add data, delete data, modify data, query data.


*


* @author Msquirrel


*


*/


public class Sqliteactivity extends activity {





Private Button btncteatedatabase = null;


Private Button btnadd = null;


Private Button btnupdate = null;


Private Button btnquery = null;


Private Button btndelete = null;





Private Sqlitedatabase db = null;





@Override


public void OnCreate (Bundle savedinstancestate) {


Super.oncreate (savedinstancestate);


Setcontentview (R.layout.activity_sqlite);


Open or create a test.db database


db = Openorcreatedatabase ("Test.db", context.mode_private, NULL);


Initview ();


Setlistener ();


}





private void Initview () {


TODO auto-generated Method Stub


Btncteatedatabase = (Button) Findviewbyid (r.id.btn_create_database);


Btnadd = (Button) Findviewbyid (R.id.btn_add);


Btnupdate = (Button) Findviewbyid (r.id.btn_update);


Btnquery = (Button) Findviewbyid (r.id.btn_query);


Btndelete = (Button) Findviewbyid (r.id.btn_delete);


}





private void Setlistener () {


TODO auto-generated Method Stub


Btncteatedatabase.setonclicklistener (New Myonclicklistener ());


Btnadd.setonclicklistener (New Myonclicklistener ());


Btnupdate.setonclicklistener (New Myonclicklistener ());


Btnquery.setonclicklistener (New Myonclicklistener ());


Btndelete.setonclicklistener (New Myonclicklistener ());


}





Private class Myonclicklistener implements Onclicklistener {





@Override


public void OnClick (View v) {


TODO auto-generated Method Stub





Switch (V.getid ()) {


Case R.id.btn_create_database:


Db.execsql ("DROP TABLE IF EXISTS person");


Create a person table


Db.execsql ("CREATE TABLE person (_id INTEGER PRIMARY KEY autoincrement, name VARCHAR, age SMALLINT)");


Break


Case R.id.btn_add:


person who = new person ();


Person.setname ("John");


Person.setage (19);


Inserting data


Db.execsql ("INSERT into person VALUES (NULL,?,?)",


New object[] {person.getname (), Person.getage ()});


Break


Case R.id.btn_update:


Person Person2 = new person ();


Person2.setname ("John");


Person2.setage (20);


Contentvalues CV = new Contentvalues ();


Cv.put ("Age", Person2.getage ()); Modify Content


Db.update ("Person", CV, "name =?",


New string[] {person2.getname ()});


Break


Case R.id.btn_query:


Cursor C = Db.rawquery ("select * from person where name=?", New string[]{"John"});


while (C.movetonext ()) {


int id = integer.parseint (c.getstring (C.getcolumnindex ("_id"));


String name =c.getstring (C.getcolumnindex ("name"));


String age = c.getstring (C.getcolumnindex (' age '));


LOG.V ("Test", "ID:" + ID + "Name:" + name + "Age:" + age);


}


Break


Case R.id.btn_delete:


String whereclause = "_id=?";/ /delete Condition


String[] Whereargs = {"1"};//deleted condition parameters


Db.delete ("person", Whereclause,whereargs)//execute Delete





Break


Default


Break


}





}





}





}



Step Three:


The code is as follows




Package Com.msquirrel.model;


/**


* Entity class


* @author Msquirrel


*


*/


public class Person {





private int _id;


private String name;


private int age;





public int get_id () {


return _id;


}


public void set_id (int _id) {


this._id = _id;


}


Public String GetName () {


return name;


}


public void SetName (String name) {


THIS.name = name;


}


public int getage () {


return age;


}


public void Setage (int age) {


This.age = age;


}





}


Finally, we should remember to turn off the sqlitedatabase.


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.