Loei 8 days Fast mastering Android Video tutorial--17_ CREATE DATABASE and complete data addition and deletion check

Source: Internet
Author: User

1, we first look at the whole project

The project is also a framework for using MVC

 PackageDB;ImportAndroid.content.Context;Importandroid.database.sqlite.SQLiteDatabase;ImportAndroid.database.sqlite.SQLiteOpenHelper;ImportAndroid.widget.Toast; Public classDbopenhelperextendssqliteopenhelper{ PublicDbopenhelper (Context context) {Super(Context, "wy.db",NULL, 1); } @Override Public voidonCreate (Sqlitedatabase db) {Db.execsql ("CREATE TABLE person (PersonID Integer primary key autoincrement, name varchar (), phone varchar () NULL)"); } @Override Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) {    }}

Interface classes for Action tables:

 PackageService;ImportAndroid.content.Context;ImportAndroid.database.Cursor;Importandroid.database.sqlite.SQLiteDatabase;ImportAndroid.util.Log;Importjava.util.ArrayList;Importjava.util.List;ImportDb.dbopenhelper;Importdomain. person; Public classPersonservice {PrivateDbopenhelper Dbopenhelper;  PublicPersonservice (Context context) { This. Dbopenhelper =NewDbopenhelper (context); }     Public voidSave (person person) {Sqlitedatabase db=dbopenhelper.getwritabledatabase (); Db.execsql (INSERT into person (name, phone) VALUES (?,?) ",                Newobject[]{person.getname (), Person.getphone ()}); }    /*** Delete Record *@paramName Record ID*/     Public voidDelete (String name,string phone) {sqlitedatabase db=dbopenhelper.getwritabledatabase (); Db.execsql ("Delete from the person where name=?" And phone=? ",NewObject[]{name,phone}); }    /*** Update record *@param Person*/     Public voidUpdate (person person,string name,string phone) {sqlitedatabase db=dbopenhelper.getwritabledatabase (); Db.execsql ("Update person set name=?,phone=?" where Name=? and phone=? ",                Newobject[]{person.getname (), Person.getphone (), name,phone}); }    /*** Enquiry Record *@paramName Record ID *@return     */     PublicPerson Find (String name,string phone) {sqlitedatabase db=dbopenhelper.getreadabledatabase (); Cursor Cursor= Db.rawquery ("Select * from the person where name=?") and phone =? ",NewString[]{name,phone}); if(Cursor.movetonext ()) {intPersonID = Cursor.getint (Cursor.getcolumnindex ("PersonID")); String name1= Cursor.getstring (Cursor.getcolumnindex ("name")); String Phone1= Cursor.getstring (Cursor.getcolumnindex ("Phone")); return NewPerson (name1, phone1);        } cursor.close (); return NULL; }    /*** Paging for records *@paramOffset skips the previous number of records *@paramMaxresult How many records are taken per page *@return     */     PublicList<person> Getscrolldata (intOffsetintMaxresult) {List<Person> persons =NewArraylist<person>(); Sqlitedatabase DB=dbopenhelper.getreadabledatabase (); Cursor Cursor= Db.rawquery ("SELECT * from Person ORDER by PersonID ASC limit?,?",                Newstring[]{string.valueof (offset), string.valueof (Maxresult)});  while(Cursor.movetonext ()) {intPersonID = Cursor.getint (Cursor.getcolumnindex ("PersonID")); /*This can also be written as * String name = cursor.getstring (1);             String phone = cursor.getstring (2); The default table comes with an ID field of 0, name is all 1 for the first field, and phone is a second field of 2*/String name= Cursor.getstring (Cursor.getcolumnindex ("name")); String Phone= Cursor.getstring (Cursor.getcolumnindex ("Phone")); Persons.add (NewPerson (name, phone));        } cursor.close (); returnpersons; }    /*** Get the total number of records *@return     */     Public LongGetCount () {Sqlitedatabase db=dbopenhelper.getreadabledatabase (); Cursor Cursor= Db.rawquery ("SELECT count (*) from person",NULL);        Cursor.movetofirst (); Longresult = Cursor.getlong (0);//after statistics there is only one default field, so 0Cursor.close (); returnresult; }}

The person class object is:

/*** File name: Person.java * Copyright: Copyright (C) China Electric 30 Department three * Description: * Modified by: Wei.yuan * Modified: 2015/1/9 * Modified: New*/ Packagedomain;/*** Project Name: SQLLite1 * Class Description: * Creator: Wei.yuan * created in: 2015/1/9 11:07 * Modified by: Wei.yuan * Modified: 2015/1/9 11:07 * Modified NOTE: * Copyright: All rights Reserved (C ) China Electric 30 Department three*/ Public classperson{PrivateString name; PrivateString Phone; @Override PublicString toString () {return"person{" + "name=" + name + ' \ ' + ", phone= ' + phone + ' \ ' + '} '; }     PublicPerson (string name, String phone) { This. Name =name;  This. Phone =phone; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString Getphone () {returnphone; }     Public voidSetphone (String phone) { This. Phone =phone; }}

We can write a test tool class to test the functionality above:

The test class for Android must inherit Androidtestcase, and the method of the class must start with test

 PackageTestsqllite;Importandroid.test.AndroidTestCase;ImportAndroid.util.Log;ImportAndroid.widget.Toast;Importjava.util.List;ImportDb.dbopenhelper;Importdomain. person;Importservice. Personservice; Public classTestsqlliteextendsAndroidtestcase {Final  StaticString TAG = "Weiyuan";  Public  voidTestcreatedb () {dbopenhelper dbopenhelper=NewDbopenhelper (GetContext ());        Dbopenhelper.getwritabledatabase (); LOG.I (TAG,"Database Creation succeeded"); }     Public voidTestsave ()throwsexception{Personservice Service=NewPersonservice ( This. GetContext ());  for(inti = 0;i<20;i++) {Service.save (NewPerson ("Weiyuan" +i, "12345" +i)); } log.i (TAG,"Data saved successfully"); }      /*The main search is the name and the phone to find together, as long as the name and telephone, only the correct*/     Public voidTestfind ()throwsexception{Personservice Service=NewPersonservice ( This. GetContext ()); Person Person= Service.find ("Chendong", "456789");        LOG.I (TAG, person.tostring ()); LOG.I (TAG,"Data Lookup succeeded"); }    /*Delete a record*/     Public voidTestdelete ()throwsexception{Personservice Service=NewPersonservice ( This. GetContext ()); Service.delete ("Weiyuan1", "123451"); LOG.I (TAG,"Data deletion succeeded"); }    /*give a new record*/     Public voidTestupdate ()throwsexception{Personservice Service=NewPersonservice ( This. GetContext ()); Service.update (NewPerson ("Chendong", "456789"), "weiyuan2", "123452"); LOG.I (TAG,"Data modification succeeded"); }        /*Get Paged Data*/     Public voidTestscrolldata ()throwsexception{Personservice Service=NewPersonservice ( This. GetContext ()); List<Person> persons = Service.getscrolldata (0, 5);  for(person person:persons) {log.i (TAG, person.tostring ()); }    }     Public voidTestcount ()throwsexception{Personservice Service=NewPersonservice ( This. GetContext ()); Longresult =Service.getcount (); LOG.I (TAG, result+""); }}

Loei 8 days Fast mastering Android Video tutorial--17_ CREATE DATABASE and complete data addition and deletion check

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.