SQLite additions and deletions to search

Source: Internet
Author: User

 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

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.