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]"); } }