Android -- SQLite (add, delete, modify, query) Operation

Source: Internet
Author: User

Five classes are required: 1. entity class: Person. java2. abstract class: SQLOperate. java (encapsulates Database Operations) 3. helper class: DBOpenHelper. java (inherit from SQLiteOpenHelper) 4. implementation class: SQLOperateImpl. java (implements the abstract class SQLOperate. java) 5. test class: Test. java (inheriting AndroidTestCase) 1. person. java [html] 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 () {retu Rn 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 [html] package com. mrzhu. sqltite; import java. util. list;/*** add, delete, modify, and query * @ author ZLQ **/public interface SQLOpe Rate {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 [html] package com. mrzhu. sqltite; import android. content. context; import android. database. sqlite. SQLiteDatabase; import android. database. sqlite. SQLiteOpenHelper;/*** helper class ** @ author ZLQ **/public class DBOpneHelper extends S QLiteOpenHelper {private static final int VERSION = 1; // The 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 "; // public static final String NAME = "name"; // NAME of the column in the table // statement for creating a database, STUDENT_TABLE, _ ID, add a space before and after NAME: private static final String CREATE_TABLE = "create table" + STUDENT_TABLE + "(" + _ I D + "Integer primary key autoincrement," + NAME + "text)"; public DBOpneHelper (Context context) {super (context, DB_NAME, null, VERSION );} // call @ Override public void onCreate (SQLiteDatabase db) {db.exe cSQL (CREATE_TABLE );} // called during version upgrade @ Override public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {}} 4. SQLOperateImpl. java [html] 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 partition (Context context) {dbOpenHelper = new DBOpneHelper (context);}/*** added, insert data into the database using insert */public v Oid 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, delete data by id */public void delete (int id) {SQLiteDatabase db = dbOpenHelper. getWritableDatabase (); db. delete (DBOpneHelper. STUDENT_TABL E, DBOpneHelper. _ ID + "=? ", New String [] {String. valueOf (id)});}/*** modify the data of 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 ()});}/*** query all data in the table */public List <Person> find () {List <Person> persons = null; SQLiteDatabase db = dbOpenHelper. getReadableDatabase (); Cursor cursor = db. query (DBOpneHelper. STUDENT_TABLE, 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 data of the specified id */public Person findById (int id) {SQLiteDatabase db = dbO PenHelper. getReadableDatabase (); Cursor cursor = db. query (DBOpneHelper. STUDENT_TABLE, null, DBOpneHelper. _ ID + "=? ", New String [] {String. valueOf (id)}, 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 in AndroidManifest. <application> </application> in xml (targetPackage is the package name of the current project) <instrumentationandroid: targetPackage = "com. mrzhu. sqltite "android: name =" android. test. </InstrumentationTestRunner "> </instrumentation> in <application> </application>, add <uses-library android: name =" android. test. runner "/> [html] 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 ());}}

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.