Android SQL statement to implement database additions and deletions

Source: Internet
Author: User

This article describes the database additions and deletions in Android

Review SQL Syntax:

* Increase

Insert into info (name,phone) VALUES (' Wuyudong ', ' 111 ')

* Delete

Delete from person where name = ' Wuyudong '

* Change

Update person set number= ' 119 ' where name= ' Wuyudong '

* Check

SELECT * FROM person

SELECT * FROM person where name= ' Wuyudong '

The database file is in the/data/data/package name/databases/xxx.db

The following uses the code to complete the related operation

First define a person class

 PackageCom.wuyudong.db.domain; Public classPerson {Private intID; PrivateString name; PrivateString number;  PublicPerson () {} PublicPerson (intID, string name, string number) {         This. ID =ID;  This. Name =name;  This. Number =Number ; }     Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString GetNumber () {returnNumber ; }     Public voidSetnumber (String number) { This. Number =Number ; }}

Then implement the code for the relevant operation:

 PackageCom.wuyudong.db.dao;Importjava.util.ArrayList;Importjava.util.List;ImportAndroid.content.Context;ImportAndroid.database.Cursor;Importandroid.database.sqlite.SQLiteDatabase;ImportCom.wuyudong.db.PersonSQLiteOpenHelper;ImportCom.wuyudong.db.domain.Person; Public classPersondao {PrivatePersonsqliteopenhelper Helper;  PublicPersondao (Context context) {Helper=NewPersonsqliteopenhelper (context); }    /*** Add a record to the database * *@paramName * Name *@paramnumber * Phone*/     Public voidAdd (string name, string number) {Sqlitedatabase db=helper.getwritabledatabase (); Db.execsql (Insert into person (name,number) VALUES (?,?),                Newobject[] {name, number});    Db.close (); }    /*** Query record is present * *@paramName * Name return true exists, false does not exist*/     Public BooleanFind (String name) {Sqlitedatabase db=helper.getreadabledatabase (); Cursor Cursor= Db.rawquery ("select * from person where name=?"),                Newstring[] {name}); Booleanresult =Cursor.movetonext ();        Cursor.close ();        Db.close (); returnresult; }    /**     *      * @paramName * Names of persons to be modified *@paramNewnumber * New number*/     Public voidUpdate (string name, String newnumber) {Sqlitedatabase db=helper.getreadabledatabase (); Db.execsql ("Update person set number=?" Where Name=? ",Newobject[]{newnumber,name});    Db.close (); }        /*** Delete a record *@paramname*/     Public voidDelete (String name) {Sqlitedatabase db=helper.getreadabledatabase (); Db.execsql ("Delete from the person where name=?",Newobject[]{name});    Db.close (); }        /*** Return all database information *@return     */     PublicList<person>FindAll () {List<Person> persons =NewArraylist<person>(); Sqlitedatabase DB=helper.getreadabledatabase (); Cursor Cursor= Db.rawquery ("SELECT * FROM person",NULL);  while(Cursor.movetonext ()) {intid = cursor.getint (cursor.getcolumnindex ("id"))); String name= Cursor.getstring (Cursor.getcolumnindex ("name")); String Number= Cursor.getstring (Cursor.getcolumnindex ("number")); Person Person=NewPerson (ID, name, number);        Persons.add (person);        } cursor.close ();        Db.close (); returnpersons; }}

The complete test code is as follows:

 Packagecom.wuyudong.db.test;Importjava.util.List;ImportCom.wuyudong.db.PersonSQLiteOpenHelper;ImportCom.wuyudong.db.dao.PersonDao;ImportCom.wuyudong.db.domain.Person;Importandroid.database.sqlite.SQLiteDatabase;Importandroid.test.AndroidTestCase; Public classTestpersondbextendsAndroidtestcase { Public voidTestcreatedb ()throwsException {personsqliteopenhelper helper=NewPersonsqliteopenhelper (GetContext ()); Sqlitedatabase DB=helper.getwritabledatabase (); }     Public voidTestadd ()throwsException {Persondao dao=NewPersondao (GetContext ()); Dao.add ("Wuyudong", "666"); }     Public voidTestfind ()throwsException {Persondao dao=NewPersondao (GetContext ()); Booleanresult = Dao.find ("Zhangsan"); Assertequals (true, result); }     Public voidTestupdate ()throwsException {Persondao dao=NewPersondao (GetContext ()); Dao.update ("Zhangsan", "655"); }     Public voidTestdelete ()throwsException {Persondao dao=NewPersondao (GetContext ()); Dao.delete ("Zhangsan"); }     Public voidTestfindall ()throwsException {Persondao dao=NewPersondao (GetContext ()); List<Person> persons =Dao.findall ();  for(person person:persons) {System.out.println (person.tostring ()); }    }}

Android SQL statement to implement database additions and deletions

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.