1, Dbadapter class: 1 Packagecom.cnzcom.android.quickdial;Importandroid.content.ContentValues;ImportAndroid.content.Context;ImportAndroid.database.Cursor;Importandroid.database.SQLException;Importandroid.database.sqlite.SQLiteDatabase;ImportAndroid.database.sqlite.SQLiteOpenHelper;ImportAndroid.util.Log;/** * * @authorZhangjie * * Database-related Operations classes*/ Public classDbadapter {/*** Database name*/ Private Static FinalString database_name = "Quickdial.db"; /*** Data table name*/ Private Static FinalString database_table = "Quickdial"; /*** Database Version*/ Private Static Final intDatabase_version = 1; /*** key_id: Primary key*/@SuppressWarnings ("Unused") Private Static FinalString key_id = "key_id"; /*** Position: location information, which means the data is the first item*/ Private Static FinalString POSITION = "POSITION"; /*** Name: Name*/ Private Static FinalString name = "Name"; /*** Phone_number: Phone number*/ Private Static FinalString phone_number = "Phone_number"; /*** IP: whether IP broadcast*/ Private Static FinalString ip = "IP"; /** * */ Private Static FinalString database_create = "CREATE table quickdial (key_id integer PRIMARY key," + "position integer," + "Name text," + "Phone_number text," + "IP INTEGER" + ");"; /** * */ Private Finalcontext Context; /** * */ PrivateDatabasehelper DBHelper; /** * */ PrivateSqlitedatabase DB; Publicdbadapter (Context ctx) {context=CTX; DBHelper=NewDatabasehelper (context); } Private Static classDatabasehelperextendsSqliteopenhelper { PublicDatabasehelper (Context context) {Super(Context, database_name,NULL, database_version); } @Override Public voidonCreate (Sqlitedatabase db) {//TODO auto-generated Method StubDb.execsql (database_create); } @Override Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) { //TODO auto-generated Method StubDb.execsql ("DROP TABLE IF EXISTS" +database_table); OnCreate (DB); } } /*** Open Database *@return * @throwsSQLException*/ PublicSqlitedatabase Open ()throwsSQLException {db=dbhelper.getwritabledatabase (); Cursor Cursor=GetAll (); if(Cursor.getcount () = = 0) {LOG.E ("Database is empty", "Insert Data"); /*** Initialize database with dummy data first*/ for(inti = 0; I < 10; i++) {Insert (I,"", "", 0); } } Else{LOG.E ("Database is not empty", "read Data"); } cursor.close (); returnDB; } /*** Close Database*/ Public voidClose () {dbhelper.close (); } /*** Inserting data into the database*/ Public LongInsertintPosition, string name, String Phone_number,intIP) {contentvalues initialvalues=Newcontentvalues (); Initialvalues.put (POSITION, POSITION); Initialvalues.put (name, name); Initialvalues.put (Phone_number, Phone_number); Initialvalues.put (IP, IP); returnDb.insert (Database_table,NULL, initialvalues); } /*** Delete data, not really delete, but will name = "", Phone_number = "", IP = 0*/ Public BooleanDeleteintposition) {contentvalues initialvalues=Newcontentvalues (); Initialvalues.put (POSITION, POSITION); Initialvalues.put (NAME,""); Initialvalues.put (Phone_number,""); Initialvalues.put (IP,0); returnDb.update (database_table, initialvalues, POSITION + "=" + POSITION,NULL) > 0; //return Db.delete (database_table, POSITION + "=" + POSITION, NULL) > 0; } /*** Change Data*/ Public BooleanUpdateintPosition, string name, String Phone_number,intIP) {contentvalues initialvalues=Newcontentvalues (); //initialvalues.put (POSITION, POSITION);initialvalues.put (name, name); Initialvalues.put (Phone_number, Phone_number); Initialvalues.put (IP, IP); returnDb.update (database_table, initialvalues, POSITION + "=" + POSITION,NULL) > 0; } Publiccursor GetAll () {cursor cur=db.query (database_table,NULL,NULL,NULL,NULL,NULL,NULL); returncur; } PublicCursor Get (LongROWID)throwsSQLException {Cursor cur= Db.query (true, Database_table,Newstring[] {POSITION, NAME, Phone_number, IP}, POSITION+ "=" + RowId,NULL,NULL,NULL,NULL,NULL); if(cur! =NULL) {Cur.movetofirst (); } returncur; } }2. Call in activity: 1 Publicdbadapter M_dbadapter;m_dbadapter=NewDbadapter ( This); M_dbadapter.open ();
Article source
Http://www.cnblogs.com/Jackeyzhang/archive/2011/06/02/2068459.html
Access instances of the sqlite-database (RPM)