João Rocha da Silva
João Rocha da Silva

Reputation: 4309

SPARQL Update example for updating more than one triple in a single query

Can anyone point me to a valid UPDATE statement in SPARQL in any documentation (be it W3C, virtuoso, semantic web page, or your own custom code, etc?

It has to be complete with WHERE specifications and more than one triple being updated in a single query.

Thanks.

EDIT / Example:

Here is my current code, which has the goal of replacing all current values of dc:creator, dc:title and dc:description with the ones specified in the INSERT clause. Is this query correct, both in logic and syntax?

WITH GRAPH  <http://127.0.0.1:3000/dendro_graph>  
DELETE 
{ 
  :teste  dc:creator      ?o0 .
  :teste  dc:title        ?o1 .
  :teste  dc:description  ?o2 .
}
INSERT 
{ 
  :teste  dc:creator      "Creator%201" .
  :teste  dc:creator      "Creator%202" .
  :teste  dc:title        "Title%201" .
  :teste  dc:description  "Description%201" .
} 
WHERE 
{ 
  :teste  dc:creator      ?o0 .
  :teste  dc:title        ?o1 .
  :teste  dc:description  ?o2 .
} 

Upvotes: 8

Views: 20576

Answers (4)

Jo&#227;o Rocha da Silva
Jo&#227;o Rocha da Silva

Reputation: 4309

Hallelujah thank God.

After 3 days hitting my head on the wall I believe I have the solution, which is like a work around. First you make the DELETE statement, then you use the semicolon to specify the triples you want to insert. Its still two operations, but at least they are controlled by the query server, i. e. you do not have to perform two separate requests, one for DELETE other for UPDATE (very dangerous).

Here is my working code, now (apparrently) fixed:

WITH GRAPH  <http://127.0.0.1:3000/dendro_graph>  
DELETE 
{ 
  :teste  dc:creator  ?o0 .
  :teste  dc:title    ?o1 .
} 
WHERE 
{ 
  :teste  dc:creator  ?o0 .
  :teste  dc:title    ?o1 .
}; 

<------NOTE THE SEMICOLON at the end of the first part

INSERT DATA
{ 
  :teste   dc:creator   "criador%20do%20teste"  .
  :teste   dc:creator   "second%20criador%20do%20teste"  .
  :teste   dc:creator   "third%20criador%20do%20teste"  .
  :teste   dc:creator   "fourth%20criador%20do%20teste"  .
  :teste   dc:title     "t1"  .
  :teste   dc:title     "t3"  .
  :teste   dc:title     "second%20title"  .
}

Upvotes: 3

Jeen Broekstra
Jeen Broekstra

Reputation: 22053

It's still not entirely clear to me what you're trying to achieve and why the example update that you give is not doing what you want, but if the aim is to have an update that replaces triples for a given subject, you could do something like this:

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
@prefix ex: <http://example.org/> 

DELETE {?s ?p ?o}
INSERT {?s ex:title "foo" ;
           ex:description "bar" ;
           rdf:type ex:FooBar .  
       }
WHERE  { ?s ?p ?o . 
         FILTER (?s = ex:subject1) 
}

The above operation will delete all existing triples with ex:subject1 as the subject, and insert a new title, description, and type.

Or if you don't like filters, you can also formulate the above like this:

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
@prefix ex: <http://example.org/> 

DELETE {ex:subject1 ?p ?o}
INSERT {ex:subject1 ex:title "foo" ;
                    ex:description "bar" ;
                    rdf:type ex:FooBar .  
}
WHERE  { 
        ex:subject1 ?p ?o . 
}

If you only want to delete specific properties for a given subject (rather than all triples with that subject), you can modify the operation like so:

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
@prefix ex: <http://example.org/> 

DELETE {ex:subject1 ex:title ?t ;
                    ex:description ?d ; 
                    rdf:type ?c . 
       }
INSERT {ex:subject1 ex:title "foo" ;
                    ex:description "bar" ;
                    rdf:type ex:FooBar .  
}
WHERE  { 
        ex:subject1 ex:title ?t ;
                    ex:description ?d ;
                    rdf:type ?c . 
}

Upvotes: 20

Samy Nathan
Samy Nathan

Reputation: 58

sparql insert is not possible .the sparql processing for already mapping & dump rdf files . Its a only query processing for linked data or graph data.not database for like mysql,sql,etc...

the rdf file for subject object predicate format.

the sparql query for select * where{?s ?p ?o. }get result

Upvotes: -4

axblount
axblount

Reputation: 2672

In SPARQL updates are represented as a DELETE followed by an INSERT:

http://www.w3.org/TR/2010/WD-sparql11-update-20100126/#t413

PREFIX foaf:  <http://xmlns.com/foaf/0.1/>

WITH <http://example/addresses>
DELETE { ?person foaf:firstName 'Bill' }
INSERT { ?person foaf:firstName 'William' }
WHERE
  { ?person a foaf:Person .
    ?person foaf:firstName 'Bill'
  }

Upvotes: 5

Related Questions