简体   繁体   English

使用SPARQL查询在本体中插入数据类型dataTime

[英]Datatype dataTime insertion in ontology with SPARQL queries

I am manually trying to insert the data in owl file with following SPARQL command: 我正在尝试使用以下SPARQL命令在owl文件中插入数据:

qry = "PREFIX : <http://www.example.com/tempsensor#>" + 
       "INSERT DATA" +
           "{" + 
 ":ind1 :locatedIn :Delhi ;" + ":onDate "+ "2014-10-01T00:10:10"^^xsd:dateTime +" ;" + ":measures 13 ;" + " :hasUnit Celsius   ." + "}" ;
        UpdateAction.parseExecute(qry,ontmod);

On running, I am getting exception: 在运行时,我遇到异常:

Encountered " <INTEGER> "10 "" at line 1, column 96. Was expecting one of:
"graph" ...
"}" ...
";" ...
"," ...
"." ...
at com.hp.hpl.jena.sparql.lang.ParserSPARQL11Update._parse(ParserSPARQL11Update.java:78)

How should I format datetime so that sparql will allow to execuate the query. 我应该如何格式化日期时间,以便sparql将允许执行查询。 Ontology used is give at link . 使用的本体在链接中给出

If you print your created query string you should quickly be able to see that it is invalid ie 如果您打印您创建的查询字符串,您应该很快就能看到它无效,即

System.out.println(qry);

The problem is that you haven't put quotes around your date time constant as is required for literals in SPARQL. 问题是您没有像SPARQL中的文字要求那样在日期时间常数周围加上引号。

So your update needs to look more like this: 因此,您的更新需要看起来像这样:

qry = "PREFIX : <http://www.example.com/tempsensor#>\n" + 
      "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n" +
      "INSERT DATA\n" +
      "{\n" + 
      ":ind1 :locatedIn :Delhi ;\n" + 
      ":onDate \"2014-10-01T00:10:10\"^^xsd:dateTime ;\n" + 
      ":measures 13 ;" + " :hasUnit Celsius .\n" + 
      "}" ;

Note the need to use \\" to escape the quotes so Java doesn't interpret them as the start/end of a string. 请注意,需要使用\\"来对引号进行转义,因此Java不会将其解释为字符串的开头/结尾。

I also added \\n ie newlines into your string as this will help the parser give you a more meaningful error message with a more precise error location than the line 1, column 96 you get with the existing query. 我还在字符串中添加了\\n即换行符,因为这将有助于解析器为您提供一条比现有查询的line 1, column 96更有意义的错误消息,并提供更精确的错误位置。

Generally if you need to inject constants into a query/update you are better off using the Parameterized SPARQL String support in Jena which is considerably less error prone and not vulnerable to SPARQL injection as your current approach is. 通常,如果您需要在查询/更新中注入常量,那么最好使用Jena中的“ 参数化SPARQL字符串”支持,这种支持的出错率要低得多,并且不像当前方法那样容易受到SPARQL注入的影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM