Android--sqlite (Add, delete, change, check) operation Instance Code _android

Source: Internet
Author: User
Tags sqlite

5 classes are required:

1. Entity class: Person.java

2. Abstract class: Sqloperate.java (encapsulates the operation of the database)

3. Assistant class: Dbopenhelper.java (Inheriting Sqliteopenhelper)

4. Implementation class: Sqloperateimpl.java (Implement abstract class Sqloperate.java)

5. Test class: Test.java (Inherit androidtestcase)


1.person.java

Copy Code code as follows:

Package com.mrzhu.sqltite;

public class Person {

private int _id;
private String name;

public int getId () {
return _id;
}

public void setId (int _id) {
this._id = _id;
}

Public String GetName () {
return name;
}

public void SetName (String name) {
THIS.name = name;
}

@Override
Public String toString () {
return "person [id=" + _id + ", name=" + name + "]";
}

Public person () {
Super ();
}

public person (int _id, String name) {
Super ();
this._id = _id;
THIS.name = name;
}
}

2.sqloperate.java

Copy Code code as follows:

Package com.mrzhu.sqltite;

Import java.util.List;

/**
* Check and delete
* @author ZLQ
*
*/
Public interface Sqloperate {
public void Add (person P);
public void Delete (int id);
public void Updata (person P);
Public list<person> find ();
Public person FindByID (int id);
}

3.dbopenhelper.java

Copy Code code as follows:

Package com.mrzhu.sqltite;

Import Android.content.Context;
Import Android.database.sqlite.SQLiteDatabase;
Import Android.database.sqlite.SQLiteOpenHelper;

/**
* Assistant Class
* @author ZLQ
*
*/
public class Dbopnehelper extends Sqliteopenhelper {

private static final int version = 1;//version
private static final String db_name = "people.db";//Database name
public static final String student_table = "STUDENT";//Table name
public static final String _id = "_id";//Column name in table
public static final String name = "name";//Column name in table
Create a database statement, student_table,_id, add spaces around name
private static final String create_table = "CREATE TABLE" + student_table + ("+ _id +" Integer primary key Autoincre ment, "+ NAME +" text);

Public Dbopnehelper {
Super (context, db_name, NULL, VERSION);
}

Called when the database was first created
@Override
public void OnCreate (Sqlitedatabase db) {
Db.execsql (create_table);
}

Called when version is upgraded
@Override
public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {

}

}

4.sqloperateimpl.java

Copy Code code as follows:

Package com.mrzhu.sqltite;

Import java.util.ArrayList;
Import java.util.List;

Import android.content.ContentValues;
Import Android.content.Context;
Import Android.database.Cursor;
Import Android.database.sqlite.SQLiteDatabase;

public class Sqloperateimpl implements sqloperate{

Private Dbopnehelper Dbopenhelper;

Public Sqloperateimpl {
Dbopenhelper = new Dbopnehelper (context);
}

/**
* increase, insert data into the database with insert
*/
public void Add (person p) {
Sqlitedatabase db = Dbopenhelper.getwritabledatabase ();
Contentvalues values = new Contentvalues ();
Values.put (dbopnehelper._id, P.getid ());
Values.put (Dbopnehelper.name, P.getname ());
Db.insert (dbopnehelper.student_table, null, values);
}

/**
* Delete the data by ID
*/
public void Delete (int id) {
Sqlitedatabase db = Dbopenhelper.getwritabledatabase ();
Db.delete (dbopnehelper.student_table, dbopnehelper._id + "=?", new String[]{string.valueof (ID)});
}

 /**
  * Change the data for the specified ID
  */
 public void Updata (person p) {
   Sqlitedatabase db = Dbopenhelper.getwritabledatabase ();
  contentvalues values = new Contentvalues ();
  values.put (dbopnehelper._id, P.getid ());
  values.put (Dbopnehelper.name, P.getname ());
  db.update (dbopnehelper.student_table, values, dbopnehelper._id + "=?", New String[]{string.valueof ( P.getid ())});
 }

 /**
  * Check, all data in the query table
  */
 public list<person> Find () {
  list <Person> persons = NULL;
  sqlitedatabase db = Dbopenhelper.getreadabledatabase ();
  cursor Cursor = db.query (dbopnehelper.student_table, NULL, NULL, NULL, NULL, NULL, NULL);
  if (cursor!= null) {
   persons = new arraylist<person> ();
   while (Cursor.movetonext ()) {
    person person = new Person ();
    int _id = Cursor.getint (Cursor.getcolumnindex (dbopnehelper._id));
    string name = cursor.getstring (Cursor.getcolumnindex (dbopnehelper.name));
    person.setid (_id);
    person.setname (name);
    persons.add (person);
   }
  }
  return persons;
 }

/**
* Query the data for the specified ID
*/
Public person FindByID (int id) {
Sqlitedatabase db = Dbopenhelper.getreadabledatabase ();
Cursor Cursor = db.query (dbopnehelper.student_table, NULL, dbopnehelper._id + "=?", new String[]{string.valueof (ID)}, NULL, NULL, NULL);
person person = null;
if (cursor!= null && cursor.movetofirst ()) {
person = new person ();
int _id = Cursor.getint (Cursor.getcolumnindex (dbopnehelper._id));
String name = cursor.getstring (Cursor.getcolumnindex (dbopnehelper.name));
Person.setid (_id);
Person.setname (name);
}
return person;
}
}

5.test.java

Add to <application></application> outside of Androidmanifest.xml

(Targetpackage is the package name of the current project)

<instrumentation

Android:targetpackage= "Com.mrzhu.sqltite"

Android:name= "Android.test.InstrumentationTestRunner" >

</instrumentation>


Add <uses-library android:name= "Android.test.runner" to the <application></application>/>

Copy Code code as follows:

Package com.mrzhu.sqltite;

Import java.util.List;

Import Android.test.AndroidTestCase;
Import Android.util.Log;

public class Test extends Androidtestcase {
public void Testadd () throws exception{
Sqloperateimpl test = new Sqloperateimpl (GetContext ());
person person = new person (2, "Peter");
Test.add (person);
}

public void Testdelete () throws exception{
Sqloperateimpl test = new Sqloperateimpl (GetContext ());
Test.delete (1);
}

public void Testupdata () throws exception{
Sqloperateimpl test = new Sqloperateimpl (GetContext ());
person person = new person (1, "Tom");
Test.updata (person);
}

public void Testfind () throws exception{
Sqloperateimpl test = new Sqloperateimpl (GetContext ());
list<person> persons = Test.find ();
for (person Person:persons) {
LOG.I ("System.out", person.tostring ());
}
}

public void Testfindbyid () throws exception{
Sqloperateimpl test = new Sqloperateimpl (GetContext ());
Person person = Test.findbyid (2);
LOG.I ("System.out", person.tostring ());
}
}

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.