Because of the work needs, the database backup contacts added to the address book, generally have hundreds of records, insert a data system default commit once, so that the efficiency is reduced, if all the data are added and then the commit efficiency is not the same, this requires a transaction
No way to add a transaction, add one record at a time
/** * Single Add Data * * @param contacts * @return */public boolean Add (tb_contacts contacts) {Sqlitedatabase db = HELPER.GETW Ritabledatabase (); Contentvalues values = new Contentvalues (), Values.put ("name", Contacts.getname ()), Values.put ("number", Contacts.getnumber ()); Long result = Db.insert ("tb_contacts", null, values); Globalconstants.printlog_d ("[contactsdao->add] result =" + result);d b.close (); if (Result! =-1) return true; Elsereturn false;}
Tb_contacts is an entity class
public class Tb_contacts {private string Name;private string Number;public tb_contacts () {super ();} Public tb_contacts (string name, string number) {super (); this.name = Name;this.number = number;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String GetNumber () {return number;} public void Setnumber (String number) {this.number = number;} @Overridepublic String toString () {return "tb_contacts [name=" + name + ", number=" + number + "]";}}
Add more than 200 records for more than two minutes
Look at the following method
/** * Bulk Add Data * * @param cursor * @return */public boolean Add (cursor cursor) {Sqlitedatabase db = Helper.getwritableda Tabase (); Long result = 0;db.begintransaction (); while (Cursor.movetonext ()) {Contentvalues values = new Contentvalues (); String ContactName = cursor.getstring (Cursor.getcolumnindex (phone.display_name)); String Contactnumber = cursor.getstring (Cursor.getcolumnindex (Phone.number)); Values.put ("name", ContactName); Values.put ("number", contactnumber); result = Db.insert ("tb_contacts", null, values); Globalconstants.printlog_d ("[contactsdao->add] Cursor result =" + result + "number =" + Contactnumber);} Db.settransactionsuccessful (); Set transaction success, do not set automatically rollback does not commit db.endtransaction (), Cursor.close ();d b.close (), if (Result! =-1) return True;elsereturn false;}
The parameter passed in is the data obtained from the query
Cursor contactscursor = getactivity (). Getcontentresolver (). query (Phone.content_uri, NULL, NULL, NULL, NULL); Read Contact Contacts.add (contactscursor);
The same data will be available in just more than 10 seconds, and the key is the words.
1.db.begintransaction (); Turn on transactions before looping
2.db.settransactionsuccessful (); Call after Loop ends
3.db.endtransaction (); Last release transaction
The above is the operation of the general database, the Address book has a dedicated contentprovider batch operation
First, look at one record operation
/** * Add a new contact to the database * * @param name * @param number */public static void Addcontact (string name, string number) {Contentvalu ES values = new contentvalues ();//A null insertion is performed to Rawcontacts.content_uri first to obtain the Rawcontactiduri Rawcontacturi returned by the system = Mcontext.getcontentresolver (). Insert (Rawcontacts.content_uri, values); Long Rawcontactid = Contenturis.parseid ( Rawcontacturi);//Insert name Data values.clear (); Values.put (data.raw_contact_id, Rawcontactid); Values.put ( Data.mimetype, Structuredname.content_item_type);//Content Type Values.put (Structuredname.given_name, NAME); Mcontext.getcontentresolver (). Insert (contactscontract.data.content_uri,values);//Insert phone data values.clear (); Values.put (data.raw_contact_id, Rawcontactid); Values.put (Data.mimetype, Phone.content_item_type); Values.put ( Phone.number, number); Values.put (Phone.type, phone.type_mobile); Mcontext.getcontentresolver (). Insert ( contactscontract.data.content_uri,values);}
More than 200 records, almost seven minutes.
Use the following bulk add method in less than two minutes
/** * Bulk Add contacts * * @throws operationapplicationexception * @throws remoteexception */public static void Batchaddcontact (Li St<tb_contacts> list) throws RemoteException, operationapplicationexception {globalconstants.printlog_d ("[ Globalvariables->]batchaddcontact begin "); arraylist<contentprovideroperation> Ops = new arraylist<contentprovideroperation> (); int Rawcontactinsertindex = 0;for (tb_contacts contact:list) {rawcontactinsertindex = Ops.size ();//Have it to add ops.add to real implementations in bulk (Contentprovideroperation.newinsert (Rawcontacts.content_uri). Withvalue (Rawcontacts.account_type, NULL). Withvalue (Rawcontacts.account_name, null). Withyieldallowed (True). Build ());//Add Name Ops.add ( Contentprovideroperation.newinsert (Android.provider.ContactsContract.Data.CONTENT_URI). Withvaluebackreference ( Data.raw_contact_id,rawcontactinsertindex). Withvalue (Data.mimetype, structuredname.content_item_type). WithValue (Structuredname.display_name, Contact.getname ()). Withyieldallowed (True). Build ());//Add numberOps.add (Contentprovideroperation.newinsert (Android.provider.ContactsContract.Data.CONTENT_URI). Withvaluebackreference (Data.raw_contact_id,rawcontactinsertindex). Withvalue (Data.mimetype, Phone.CONTENT_ITEM_ TYPE). Withvalue (Phone.number, Contact.getnumber ()). Withvalue (Phone.type, Phone.type_mobile). Withvalue ( Phone.label, ""). Withyieldallowed (True). Build ()); if (OPS! = NULL) {//Real add contentproviderresult[] results = Mcontext.getcontentresolver (). Applybatch ( Contactscontract.authority, OPS);//For (Contentproviderresult result:results) {//globalconstants//. Printlog_d ("[globalvariables->]batchaddcontact"//+ result.uri.toString ());/}}}
Through the above comparison, for the database efficiency is not the same!