andriod increase, delete, change, check

Source: Internet
Author: User

Change the database additions and deletions into a single package

1*/2 Package Com.itheima28.sqlitedemo.dao;3 import java.util.ArrayList;4 import java.util.List;5 6 import Android.content.Context;7 import android.database.Cursor;8 import android.database.sqlite.SQLiteDatabase;9 Ten import Com.itheima28.sqlitedemo.dao.entities.Person; One import Com.itheima28.sqlitedemo.db.PersonSQliteOpenHelper; Apublic class Persondao {//who calls my package for this operation, just pass in my own object -Private Personsqliteopenhelper Mopenhelper; //the Help class object for the database -  the Public Persondao (context context) { -Mopenhelper =new Personsqliteopenhelper (context); -}

1 //Insert Operation2  Public voidInsert (person person) {3 //first connect the database this operation class Persondao has done, with its object Mopenhelper under the method4Sqlitedatabase db =mopenhelper.getwritabledatabase ();5 //determine if the database is open6 if(Db.isopen ()) {//If the database is open, perform the added action7 //perform actions added to the database//What is the object coming in, what is it, and what is it written in front of it?8Db.execsql ("INSERT into person (name,age) values (?,?);",Newobject[]{person.getname (), Person.getage ()});9Db.close ();//Database ShutdownTen } One}

1 //delete data by ID2  Public voidDeleteintID) {3Sqlitedatabase db = Mopenhelper.getwritabledatabase ();//get a writable database object4 if(Db.isopen ()) {//If the database is open, perform the following actions//Pass in the ID directly. Integer is an object type and int is not. So no. 5Db.execsql ("Delete from person where _id=?;",NewInteger[]{id});//6 db.close ();7 }8}

1 //Update2  Public voidUpdata (intid,string name) {3Sqlitedatabase db = Mopenhelper.getwritabledatabase ();//get a writable database object4 if(Db.isopen ()) {5Db.execsql ("updata person set name =?") where _id =?; ",Newobject[]{name,id});6 }7 db.close ();8}

1 //Query All2  PublicList<person>Queryall () {3Sqlitedatabase db = Mopenhelper.getwritabledatabase ();//get read-only database objects4 if(Db.isopen ()) {5cursor cursor = db.rawquery ("select * from person;",NULL);//query to get "cursor result set"6 if(Cursor! =NULL&& Cursor.getcount () >0) {//query set greater than 07List<person> personlist =NewArraylist<person> ();//Create a collection personlist8  while(Cursor.movetonext ()) {//table cursor moves from top to bottom9 intID =cursor.getint (0);//take the No. 0 column---IDTenString name = cursor.getstring (1);//take the 1th column---name One intAge = Cursor.getint (2);//take the 2nd column--- age APersonlist.add (NewPerson (id,name,age));//load the acquired data into the collection Personlist - } - db.close (); the returnPersonlist;//return this collection - } - db.close (); - } + return NULL; -}

1 //query for one, such as ID2  PublicPerson Queryitem (intID) {//Person Type3Sqlitedatabase db = Mopenhelper.getwritabledatabase ();//get read-only database objects4 if(Db.isopen ()) {//because Rawquery (string, string[]) type, the int ID is typed as string by id+ ""5Cursor cursor= db.rawquery ("Select _id,name,age from the person where _id=?;",NewString[]{id+ ""} );6 if(Cursor! =NULL&& Cursor.movetofirst ()) {//Movetofirst first found7 int_id = cursor.getint (0);//Get ID8String name= cursor.getstring (1);//Get name9 intAge = Cursor.getint (2);//Get AgeTen db.close (); One return NewPerson (_id,name,age); A } - } - return NULL; the } -}

andriod increase, delete, change, check

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.