Changes

Jump to navigation Jump to search
302 bytes removed ,  13:31, 2 July 2020
=Conventions=
 
{{Mark|Updated with Version 9.0:}}
All functions and errors in this module are assigned to the <code><nowiki>http://basex.org/modules/sql</nowiki></code> namespace, which is statically bound to the {{Code|sql}} prefix.<br/>
|-
| width='120' | '''Signatures'''
|{{Func|sql:connect|$url as xs:string|xs:anyURI}}<br/ >{{Func|sql:connect|$url as xs:string, $user as xs:string, $password as xs:string|xs:anyURI}}<br/ >{{Func|sql:connect|$url as xs:string, $user as xs:string, $password as xs:string, $options as map(xs:string, item()*)?|xs:anyURI}}<br/ >
|-
| '''Summary'''
| '''Examples'''
|Connects to an SQL Server and sets autocommit to {{Code|true}}:
<pre classsyntaxhighlight lang="brush:xquery">
sql:connect('dbc:sqlserver://DBServer', map { 'autocommit': true() })
</presyntaxhighlight>
|}
==sql:execute==
 
{{Mark|Updated with Version 9.0:}} Return update count for updating statements. {{Code|$options}} argument added.
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|sql:execute|$id as xs:anyURI, $statement as xs:string|item()*}}<br/>{{Func|sql:execute|$id as xs:anyURI, $statement as xs:string, $options as map(*)?|item()*}}
|-
| '''Summary'''
==sql:execute-prepared==
 
{{Mark|Updated with Version 9.0:}} Return update count for updating statements.
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|sql:execute-prepared|$id as xs:anyURI, $params as element(sql:parameters)|item()*}}<br/>{{Func|sql:execute-prepared|$id as xs:anyURI, $params as element(sql:parameters), $options as map(*)?|item()*}}
|-
| '''Summary'''
| This function executes a prepared statement with the specified {{Code|$id}}. The output format is identical to [[#sql:execute|sql:execute]]. The optional parameter {{Code|$params}} is an element {{Code|<sql:parameters/>}} representing the parameters for a prepared statement along with their types and values. The following schema shall be used:<br/ >
<pre classsyntaxhighlight lang="brush:xquery">
element sql:parameters {
element sql:parameter {
}+
}?
</presyntaxhighlight>
With {{Code|$options}}, the following parameter can be set:
* {{Code|timeout}}: query execution will be interrupted after the specified number of seconds.
|-
| '''Errors'''
|{{Error|attribute|#Errors}} an attribute different from {{Code|type}} and {{Code|null}} is set for a {{Code|<sql:parameter/>}} element.<br/ >{{Error|error|#Errors}} an error occurred while executing SQL.<br/ >{{Error|id|#Errors}} the specified connection does not exist.<br/ >{{Error|parameters|#Errors}} wrong number of {{Code|<sql:parameter/>}} elements, or no parameter type is not specified.<br/>{{Error|timeout|#Errors}} query execution exceeded timeout.<br/>{{Error|type|#Errors}} the value of a parameter cannot be converted to the specified format.
|}
A simple select statement can be executed as follows:
<pre classsyntaxhighlight lang="brush:xquery">
let $id := sql:connect("jdbc:postgresql://localhost:5432/coffeehouse")
return sql:execute($id, "SELECT * FROM coffees WHERE price < 10")
</presyntaxhighlight>
The result may look like:
<pre classsyntaxhighlight lang="brush:xml">
<sql:row xmlns:sql="http://basex.org/modules/sql">
<sql:column name="cof_name">French_Roast</sql:column>
<sql:column name="total">14</sql:column>
</sql:row>
</presyntaxhighlight>
==Prepared Statements==
A prepared select statement can be executed in the following way:
<pre classsyntaxhighlight lang="brush:xquery">
(: Establish a connection :)
let $conn := sql:connect("jdbc:postgresql://localhost:5432/coffeehouse")
(: Execute prepared statement :)
return sql:execute-prepared($prep, $params)
</presyntaxhighlight>
==SQLite==
The following expression demonstrates how SQLite can be addressed using with the [httphttps://bitbucket.org/xerial/sqlite-jdbc / Xerial SQLite JDBC driver]:
<pre classsyntaxhighlight lang="brush:xquery">
(: Initialize driver :)
sql:init("org.sqlite.JDBC"),
sql:execute($conn, "select * from person")
)
</presyntaxhighlight>
=Errors=
 
{{Mark|Updated with Version 9.0:}}
{| class="wikitable" width="100%"
|-
|{{Code|parameters}}
|Wrong number of {{Code|&lt;sql:parameter/&gt;}} elements, or No parameter type is not specified.
|-
|{{Code|timeout}}
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu