Packagecom.scme.db;Importjava.util.ArrayList;Importjava.util.List;Importcom.scme.bean.Students;ImportCom.scme.bean.Tongxunlu;ImportAndroid.content.Context;ImportAndroid.database.Cursor;Importandroid.database.sqlite.SQLiteDatabase;Importandroid.database.sqlite.SQLiteDatabase.CursorFactory;ImportAndroid.database.sqlite.SQLiteOpenHelper; Public classDBHelperextendsSqliteopenhelper {//Define database name Private Static FinalString DBNAME = "Txl.db"; //Defining database Versions Private Static Final intVERSION = 1; PublicDBHelper (Context context, String name, Cursorfactory factory,intversion) { Super(context, DBNAME, Factory, VERSION); } /*** CREATE DATABASE Tables*/@Override Public voidonCreate (Sqlitedatabase db) {String SQL1= "CREATE table user_info (_id integer primary key autoincrement,stuname text,stupwd text,stusex text,stuage integer)"; String SQL2= "CREATE table phone_info (_id integer primary key autoincrement,txlname text,txlphone text,txlemail text,txladdr text)"; Db.execsql (SQL1); Db.execsql (SQL2); } @Override Public voidOnupgrade (Sqlitedatabase arg0,intArg1,intarg2) { } /*** Registration*/ Public voidAddstu (Students stu) {String SQL= "INSERT into User_info (stuname,stupwd,stusex,stuage) VALUES (?,?,?,?)"; Sqlitedatabase DB=getwritabledatabase (); Db.execsql (SQL,Newobject[] {stu.getstuname (), Stu.getstupwd (), Stu.getstusex (), Stu.getstuage ()}); Db.close (); } /*** Login * *@paramname *@parampwd *@return */ Public BooleanLogin (string name, string pwd) {String sql= "SELECT * from user_info where stuname =" +name+ "and stupwd =" +pwd; Sqlitedatabase DB=getreadabledatabase (); Cursor C= Db.rawquery (SQL,NULL); while(C.movetonext ()) {if(c.getstring (1)! =NULL) { return true; }} c.close (); Db.close (); return false; } /*** Add a contact * *@paramTXL*/ Public voidADDTXL (Tongxunlu txl) {String SQL= "INSERT into Phone_info (TXLNAME,TXLPHONE,TXLEMAIL,TXLADDR) VALUES (?,?,?,?)"; Sqlitedatabase DB=getwritabledatabase (); Db.execsql (SQL,Newobject[] {txl.gettxlname (), Txl.gettxlphone (), Txl.gettxlemail (), Txl.gettxladdr ()}); Db.close (); } /*** Delete Contact * *@paramID*/ Public voidDELTXL (intID) {String SQL= "Delete from phone_info where _id =" +ID; Sqlitedatabase DB=getwritabledatabase (); Db.execsql (SQL); Db.close (); } /*** Modify Contact * *@paramTXL*/ Public voidUPDATETXL (Tongxunlu txl) {String SQL= "Update phone_info set txlname=?,txlphone=?,txlemail=?,txladdr=?" where _id =? "; Sqlitedatabase DB=getwritabledatabase (); Db.execsql (SQL,Newobject[] {txl.gettxlname (), Txl.gettxlphone (), Txl.gettxlemail (), txl.gettxladdr (), txl.get_i D ()}); Db.close (); } /*** Query all + Search by name * *@paramname *@return */ PublicList<tongxunlu>queryname (String name) {ArrayList<Tongxunlu> list =NewArraylist<tongxunlu>(); String SQL= "SELECT * FROM Phone_info"; if(Name! =NULL&&!name.equals ("") ) {SQL+ = "where txlname like '%" + name + "% '"; } sqlitedatabase DB=getreadabledatabase (); Cursor C= Db.rawquery (SQL,NULL); while(C.movetonext ()) {Tongxunlu TXL=NewTongxunlu (); TXL.SET_ID (C.getint (0)); Txl.settxlname (C.getstring (1)); Txl.settxlphone (C.getstring (2)); Txl.settxlemail (C.getstring (3)); Txl.settxladdr (C.getstring (4)); List.add (TXL); } c.close (); Db.close (); returnlist; } /*** Search by ID * *@paramID *@return */ PublicTongxunlu Qyeryid (intID) {Tongxunlu TXL=NewTongxunlu (); String SQL= "SELECT * from phone_info where _id =" +ID; Sqlitedatabase DB=getreadabledatabase (); Cursor C= Db.rawquery (SQL,NULL); if(C.movetonext ()) {txl.set_id (C.getint (0)); Txl.settxlname (C.getstring (1)); Txl.settxlphone (C.getstring (2)); Txl.settxlemail (C.getstring (3)); Txl.settxladdr (C.getstring (4)); } c.close (); Db.close (); returnTXL; }}
SQLite additions and deletions to search