Usage of contentvalues in URI

Source: Internet
Author: User

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 !!

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.