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

4

8

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 .
} 
Illiteracy answered 21/10, 2013 at 18:53 Comment(4)
Your example query is correct, as far as I can see. I don't quite understand what problem you're trying to solve.Golter
I want to make a simple web page where users csn update information in the graph, and not just perform selects like in dbpedia. if there is more than an instsnce of a given dc: descriptor, they have to be all deleted before replacing the values with the new ones.Townley
Yes, so? It's still not clear what is not working for you. What did the above SPARQL operation do when you tried it? What did you expect it to do? Perhaps show some example data, expected outcome, and actual outcome.Golter
It is just a simple update, as basic as it can be... Updating all the triples that have a resource X as their subject.Townley
G
20

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 . 
}
Golter answered 22/10, 2013 at 11:56 Comment(4)
Thank you very much for taking the time to provide such a detailed example. I will run some queries in the Virtuoso interface to illustrate my need, and will test your suggestions. If they work for me, this will be the accepted answer.Townley
any possible to UPDATE ?Concept skosxl:prefLabel/skosxl:literalForm ?Label and there's might no skosxl:altLabel resource and its skosxl:literalForm, then UPDATE prefLabel's label and insert altLabel resource and skosxl:literalForm together?Fracas
Will it do insert only operation if triples don't exists in db?Stope
@jigar-gajjar no, because if the triples don't exist, the WHERE clause won't match.Golter
M
5

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'
  }
Matthewmatthews answered 21/10, 2013 at 19:9 Comment(1)
Thanks, I already read that documentation. I need a query capable of updating more than one triple in a single operation. In practice, deleting all triples matching the WHERE condition and replacing them with some others. Since I want to avoid having to know the old values of all the elements in the triple, I cannot use DELETE DATA or INSERT DATA statements; I need bindings in the query.Townley
I
3

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"  .
}
Illiteracy answered 21/10, 2013 at 19:55 Comment(5)
You don't need separate operations for this (though there's nothing wrong with it in principle).Golter
If the system is used concurrently, the deletes and inserts may be performed in such an order that it may leave the database in inconsistent states. At least tjis way the server will handle the commits at the end of each couple of operation, even though there are no ACID transactions on Virtuoso as far as I know.Townley
You're missing my point. You can do this kind of update in a single SPARQL operation. You don't need to create a sequence of two or more operations, like you're doing here.Golter
Well that is what ive been trying to do for 3 days... All my google resulta for thr first 3 pages of the search are visited, so if you have a solution that does not require a sequence of operations I would love to hear it :-)Townley
The thing is that you give an example of such a solution yourself, in your question, and you're not explaining why that solution is not working for you. Nevertheless, I've tried to give you an alternative solution, see my answer.Golter
S
-4

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

Stent answered 11/2, 2016 at 10:26 Comment(2)
The whole SPARQL 1.1 Update specification describes how you insert, delete, etc., in SPARQL.Snowdrift
What Joshua says - See SPARQL 1.1Keystroke

© 2022 - 2024 — McMap. All rights reserved.