Programmatically use the SQLite database to perform crud (create read update delete) operations

Source: Internet
Author: User
Tags sqlite database

Dbadapter Database Helper Classes

 Packagecom.databses; Importandroid.content.ContentValues; ImportAndroid.content.Context; ImportAndroid.database.Cursor; Importandroid.database.SQLException; Importandroid.database.sqlite.SQLiteDatabase; ImportAndroid.database.sqlite.SQLiteOpenHelper; ImportAndroid.util.Log;  Public classDbadapter {Static FinalString Key_rowid = "_id"; Static FinalString key_name = "NAME"; Static FinalString key_email = "EMAIL"; Static FinalString TAG = "Dbadapter"; Static FinalString database_name = "MyDB"; Static FinalString database_table = "Contacts"; Static Final intDatabase_version = 1; Finalcontext Context; //To create a table SQL statement    Static FinalString database_create = "CREATE table Contacts (_id integer primary key autoincrement,name text not null,email text not n ULL) ";      Sqlitedatabase DB;            Databasehelper DBHelper;  PublicDbadapter (Context context) { This. Context =context; DBHelper=NewDatabasehelper (context); }            //Database helper class inheritance Sqliteopenhelper    Private Static classDatabasehelperextendssqliteopenhelper{ PublicDatabasehelper (Context context) {Super(Context, database_name,NULL, database_version); }            //Create a database@Override Public voidonCreate (Sqlitedatabase db) {Try{db.execsql (database_create); } Catch(Exception e) {e.printstacktrace (); }          }          //upgrading the database version@Override Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) {LOG.W (TAG,"Upgrading db from Version" +oldversion+ "to" +newversion+ ", which'll destory all old data"); Db.execsql ("DROP table if exists contacts");          OnCreate (DB); }      }            //Open Database     PublicDbadapter Open ()throwsSQLException {db=dbhelper.getwritabledatabase (); return  This; }            //Close the database     Public voidClose () {dbhelper.close (); }            //insert a Contact record     Public Longinsertcontact (String name,string email) {contentvalues Initalvalues=Newcontentvalues ();          Initalvalues.put (Key_name,name);          Initalvalues.put (Key_email, EMAIL); returnDb.insert (Database_table,NULL, initalvalues); }            //Delete a record     Public BooleanDeleteContact (LongrowId) {          returnDb.delete (database_table,key_rowid+ "=" +rowid,NULL) >0; }            //Find all Contacts     PublicCursor getallcontacts () {returnDb.query (Database_table,NewString[]{key_rowid,key_name,key_email},NULL,NULL,NULL,NULL,NULL,NULL); }            //Query a record     PublicCursor GetContact (LongROWID)throwssqlexception{cursor Cursor= Db.query (true, Database_table,NewString[]{key_rowid,key_name,key_email}, key_rowid+ "=" +rowid,NULL,NULL,NULL,NULL,NULL); if(Cursor! =NULL) {Cursor.movetofirst (); }          returncursor; }            //Update a contact     Public BooleanUpdatecontact (Longrowid,string name,string Email) {contentvalues contentvalues=Newcontentvalues ();          Contentvalues.put (Key_name, NAME);          Contentvalues.put (Key_email, EMAIL); returnDb.update (database_table, contentvalues, key_rowid+ "=" +key_rowid,NULL) >0; }  }  

Databaseactivity

 Packagecom.databses; ImportAndroid.os.Bundle; Importandroid.app.Activity; ImportAndroid.view.Menu;  Public classDatabasesactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);          Setcontentview (r.layout.activity_databases); Dbadapter Dbadapter=NewDbadapter ( This);          Dbadapter.open (); Dbadapter.insertcontact ("Weiwei", "[email protected]"); Dbadapter.insertcontact ("Jackson", "[email protected]"); }      }  
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.