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;PublicClassperson {PrivateIntIdPrivateString name;PrivateString number;PublicPerson () {}Public person (IntID, string name, string number) {This.id =IdTHIS.name =NameThis.number =Number }PublicIntGetId () {ReturnId }Publicvoid SetId (IntID) {this.id = ID;} public String getName () { return name;} public void setName (String name) { this.name = name;} public String GetNumber () { return number ;} public void Setnumber (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;PublicClassPersondao {PrivatePersonsqliteopenhelper Helper;PublicPersondao (Context context) {helper =NewPersonsqliteopenhelper (context); }/*** Add a record to the database * *@paramName * Name *@paramNumber * Phone*/PublicvoidAdd (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*/PublicBooleanFind (String name) {Sqlitedatabase db =Helper.getreadabledatabase (); cursor cursor = db.rawquery ("select * from person where name=?"),NewString[] {name});Boolean result =Cursor.movetonext (); Cursor.close (); Db.close ();ReturnResult }/*** *@paramName * Names of persons to be modified *@paramNewnumber * New Number*/PublicvoidUpdate (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*/PublicvoidDelete (String name) {Sqlitedatabase db =Helper.getreadabledatabase (); Db.execsql ("Delete from person where name=?",NewObject[]{name}); Db.close (); }/*** Return all database information *@return*/Public list<person>FindAll () {list<person> persons =New arraylist<person>(); Sqlitedatabase db = helper.getreadabledatabase (); cursor cursor = db.rawquery ("SELECT * from person", null); while (Cursor.movetonext ()) { int id = cursor.getint (cursor.getcolumnindex ("id")); String name = cursor.getstring (Cursor.getcolumnindex ("name")); String number = cursor.getstring (Cursor.getcolumnindex ("number")); Person person = New person (ID, name, number); Persons.add (person);} cursor.close (); Db.close (); return Persons;}}             

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;PublicClass TestpersondbExtendsAndroidtestcase {Publicvoid Testcreatedb ()ThrowsException {Personsqliteopenhelper helper =NewPersonsqliteopenhelper (GetContext ()); Sqlitedatabase db =Helper.getwritabledatabase (); }Publicvoid Testadd ()ThrowsException {Persondao DAO =NewPersondao (GetContext ()); Dao.add ("Wuyudong", "666"); }Publicvoid Testfind ()ThrowsException {Persondao DAO =NewPersondao (GetContext ());Boolean result = Dao.find ("Zhangsan"); Assertequals (True, result); }Publicvoid Testupdate ()ThrowsException {Persondao DAO =NewPersondao (GetContext ()); Dao.update ("Zhangsan", "655"); } public void Testdelete () throws Exception {Persondao dao = new Persondao (GetContext ()); Dao.delete ("Zhangsan" ) ; } public void Testfindall () throws Exception {Persondao dao = new Persondao (GetContext ()); list<person> persons = Dao.findall (); for 

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.