Contentvalues and hashtable are similar storage mechanisms, but the biggest difference between them is that contenvalues can only store basic types of data, such as string and INT, and cannot store objects, hashtable can store objects.
When you forget to insert data into the database, you should first have a contentvalues object, so:
Contentvalues initialvalues = new contentvalues ();
Initialvalues. Put (Key, values );
Sqlitedatabase SDB;
SDB. insert (database_name, null, initialvalues );
If the insert operation is successful, the ID of the record is returned. Otherwise,-1 is returned;
You can insert a row of data. For details, see the following code:
Public URI insert (URI Uri, contentvalues initialvalues) {<br/> If (urimatcher. Match (URI )! = Contacts) {<br/> throw new illegalargumentexception ("unknow Uri" + URI); <br/>}< br/> contentvalues values; <br/> If (initialvalues! = NULL) {<br/> values = new contentvalues (initialvalues); <br/> system. out. println ("contentvalues inserted successfully, initailvalues is not empty"); <br/>}else {<br/> values = new contentvalues (); <br/>}< br/> long now = long. valueof (system. currenttimemillis (); <br/> // set the default value <br/> If (values. containskey (contactcolumn. created) = false) {<br/> values. put (contactcolumn. created, now); <br/>}</P> <p> If (values. containskey (contact Column. name) = false) {<br/> values. put (contactcolumn. name, now); <br/>}</P> <p> If (values. containskey (contactcolumn. email) = false) {<br/> values. put (contactcolumn. email, now); <br/>}</P> <p> If (values. containskey (contactcolumn. mobile) = false) {<br/> values. put (contactcolumn. mobile, now); <br/>}</P> <p> If (values. containskey (contactcolumn. modified) = false) {<br/> values. put (contactcolumn. modif IED, now); <br/>}< br/> system. out. println ("it should be inserted successfully"); <br/> long rowid = contactsdb. insert (contacts_table, null, values); <br/> If (rowid> 0) {<br/> URI noteuri = contenturis. withappendedid (content_uri, rowid); <br/> getcontext (). getcontentresolver (). categorychange (noteuri, null); <br/> system. out. println ("it's okay here! "); <Br/> return noteuri; <br/>}< br/> throw new illegalargumentexception (" unknow Uri "+ URI); <br/>}< br/>
Of course, the above Code is operated according to the URI, so you need to understand what is going on with the code? Also understand how contentprovider stores data!
Thank you !!