Difference between revisions of "SQL Module"

From BaseX Documentation
Jump to navigation Jump to search
m (→‎Connections: fixed typo, I guess you meant *j*dbc)
 
(97 intermediate revisions by 6 users not shown)
Line 1: Line 1:
The SQL Module contains [[Querying#Functions|XQuery functions]] to access relational databases from XQuery using SQL. With this module, you can execute query, update and prepared statements, and the result sets are returned as sequences of XML elements representing tuples. Each element has attributes representing the columns returned by the SQL statement. All functions dealing with access to relational databases use the <code>sql</code> prefix, which is linked to the <code><nowiki>http://www.basex.org/sql</nowiki></code> namespace. The module will be officially supported with the upcoming <font color="red">Version 6.8</font> of BaseX.
+
This [[Module Library|XQuery Module]] contains functions to access relational databases from XQuery using SQL. With this module, you can execute query, update and prepared statements, and the result sets are returned as sequences of XML elements representing tuples. Each element has children representing the columns returned by the SQL statement.
  
==Connections==
+
This module uses JDBC to connect to a SQL server. Hence, your JDBC driver will need to be added to the classpath, too. If you work with the full distributions of BaseX, you can copy the driver into the {{Code|lib}} directory. To connect to MySQL, for example, download the [https://dev.mysql.com/downloads/connector/j/ Connector/J Driver] and extract the archive into this directory.
A connection to a relational database can be established using the function <code>sql:connect</code>. As a result a connection handle is returned. The possible signatures are:
 
  
<code><b>sql:connect</b>($url as xs:string) as xs:int</code><br/>
+
=Conventions=
<code><b>sql:connect</b>($url as xs:string, $auto-commit as xs:boolean) as xs:int</code><br/>
 
<code><b>sql:connect</b>($url as xs:string, $auto-commit as xs:boolean, $user as xs:string, $password as xs:string) as xs:int</code>
 
  
The parameter <code>$url</code> is the URL of the database and shall be of the form: <code>jdbc:<driver name>:[//<server>[/<database>]</code>. The parameter <code>$auto-commit</code> is used to indicate if the auto-commit mode shall be activated or not. Its default value is true. If the parameters <code>$user</code> and <code>$password</code> are specified, they are used as credentials for connecting to the database.
+
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/>
  
==Executing Queries==
+
=Functions=
Once a connection is established, the returned connection handle can be used to execute queries on the database. Our SQL module supports both direct queries and prepared statements.
 
  
===Direct queries===
+
==sql:init==
A query or an update statement can be executed using the function <code>sql:execute</code>:
 
  
<code><b>sql:execute</b>($connection as xs:int, $statement as xs:string) as element()*</code>
+
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signatures'''
 +
|{{Func|sql:init|$class as xs:string|empty-sequence()}}
 +
|- valign="top"
 +
| '''Summary'''
 +
|This function initializes a JDBC driver specified via {{Code|$class}}. This step might be superfluous if the SQL database is not embedded.<br/ >
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|init|#Errors}} the specified driver is not found.
 +
|}
  
The parameter <code>$connection</code> specifies the connection handle to be used and the parameter <code>$statement</code> - the statement to be executed. If a query statement is executed, the result set is returned as a sequence of <code><sql:tuple/></code> elements, each one representing a tuple. In case of an update statement an empty sequence is returned. For example, a simple select statement can be executed on the following way:
+
==sql:connect==
  
<pre class="brush:xml">
+
{| width='100%'
let $conn := sql:connect("jdbc:postgresql://localhost:5432/coffeehouse")
+
|- valign="top"
return sql:execute($conn, "SELECT * FROM coffees WHERE price < 10")
+
| width='120' | '''Signatures'''
</pre>
+
|{{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:anyURI}}<br/ >
 +
|- valign="top"
 +
| '''Summary'''
 +
|This function establishes a connection to a relational database and returns a connection id. The parameter {{Code|$url}} is the URL of the database and shall be of the form: {{Code|jdbc:<driver name>:[//<server>[/<database>]]}}. If the parameters {{Code|$user}} and {{Code|$password}} are specified, they are used as credentials for connecting to the database. The {{Code|$options}} parameter can be used to set connection options.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|error|#Errors}} an SQL exception occurred when connecting to the database.
 +
|- valign="top"
 +
| '''Examples'''
 +
|Connects to an SQL Server and sets autocommit to {{Code|true}}:
 +
<syntaxhighlight lang="xquery">
 +
sql:connect('dbc:sqlserver://DBServer', map { 'autocommit': true() })
 +
</syntaxhighlight>
 +
|}
 +
 
 +
==sql:execute==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| 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()*}}
 +
|- valign="top"
 +
| '''Summary'''
 +
| This function executes an SQL {{Code|$statement}}, using the connection with the specified {{Code|$id}}. The returned result depends on the kind of statement:
 +
* If an update statement was executed, the number of updated rows will be returned as integer.
 +
* Otherwise, an XML representation of all results will be returned.
 +
With {{Code|$options}}, the following parameter can be set:
 +
* {{Code|timeout}}: query execution will be interrupted after the specified number of seconds.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|error|#Errors}} an error occurred while executing SQL.<br/ >{{Error|id|#Errors}} the specified connection does not exist.<br/>{{Error|timeout|#Errors}} query execution exceeded timeout.<br/>
 +
|}
 +
 
 +
==sql:execute-prepared==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| 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()*}}
 +
|- valign="top"
 +
| '''Summary'''
 +
| This function executes a prepared statement with the specified {{Code|$id}}. The output format is identical to {{Function||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/ >
 +
<syntaxhighlight lang="xquery">
 +
element sql:parameters {
 +
  element sql:parameter {
 +
    attribute type { "bigdecimal" | "boolean" | "byte" | "date" | "double" | "float" |
 +
      "int" | "long" | "short" | "sqlxml" | "string" | "time" | "timestamp" },
 +
    attribute null { "true" | "false" }?,
 +
    text
 +
  }+
 +
}?
 +
</syntaxhighlight>
 +
With {{Code|$options}}, the following parameter can be set:
 +
* {{Code|timeout}}: query execution will be interrupted after the specified number of seconds.
 +
|- valign="top"
 +
| '''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}} no parameter type 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.
 +
|}
 +
 
 +
==sql:prepare==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signatures'''
 +
|{{Func|sql:prepare|$id as xs:anyURI, $statement as xs:string|xs:anyURI}}
 +
|- valign="top"
 +
| '''Summary'''
 +
|This function prepares an SQL {{Code|$statement}}, using the specified connection {{Code|$id}}, and returns the id reference to this statement. The statement is a string with one or more '?' placeholders. If the value of a field has to be set to {{Code|NULL}}, then the attribute {{Code|null}} of the {{Code|<sql:parameter/>}} element must be {{Code|true}}.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|error|#Errors}} an error occurred while executing SQL.<br/ >{{Error|id|#Errors}} the specified connection does not exist.<br/ >
 +
|}
 +
 
 +
==sql:commit==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signatures'''
 +
|{{Func|sql:commit|$id as xs:anyURI|empty-sequence()}}
 +
|- valign="top"
 +
| '''Summary'''
 +
| This function commits the changes made to a relational database, using the specified connection {{Code|$id}}.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|error|#Errors}} an error occurred while executing SQL.<br/ >{{Error|id|#Errors}} the specified connection does not exist.<br/ >
 +
|}
 +
 
 +
==sql:rollback==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signatures'''
 +
|{{Func|sql:rollback|$id as xs:anyURI|empty-sequence()}}
 +
|- valign="top"
 +
| '''Summary'''
 +
| This function rolls back the changes made to a relational database, using the specified connection {{Code|$id}}.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|error|#Errors}} an error occurred while executing SQL.<br/ >{{Error|id|#Errors}} the specified connection does not exist.<br/ >
 +
|}
 +
 
 +
==sql:close==
  
The result will look like:
+
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signatures'''
 +
|{{Func|sql:close|$id as xs:anyURI|empty-sequence()}}
 +
|- valign="top"
 +
| '''Summary'''
 +
| This function closes a database connection with the specified {{Code|$id}}.<br/>Opened connections will automatically be closed after the XQuery expression has been evaluated, but in order to save memory, it is always recommendable to close connections that are not used anymore.
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|error|#Errors}} an error occurred while executing SQL.<br/ >{{Error|id|#Errors}} the specified connection does not exist.<br/ >
 +
|}
  
<pre class="brush:xml">
+
=Examples=
<sql:tuple xmlns:sql="http://www.basex.org/sql" cof_name="French_Roast" sup_id="49" price="9.5" sales="15" total="30"/>
 
<sql:tuple xmlns:sql="http://www.basex.org/sql" cof_name="French_Roast_Decaf" sup_id="49" price="7.5" sales="10" total="14"/>
 
<sql:tuple xmlns:sql="http://www.basex.org/sql" cof_name="Colombian_Decaf" sup_id="101" price="8.75" sales="6" total="12" date="2010-10-10 13:56:11.0"/>
 
</pre>
 
  
===Prepared Statements===
+
==Direct queries==
In order to execute a prepared statement, first a prepared statement handle has to be created. This can be done with the function <code>sql:prepare</code>:
 
  
<code><b>sql:prepare</b>($connection as xs:int, $statement as xs:string) as xs:int</code>
+
A simple select statement can be executed as follows:
  
The parameter <code>$connection</code> indicates the connection handle to be used. The parameter <code>$statement</code> is a string representing an SQL statement with one or more '?' placeholders. The prepared statement can then be executed using the following signature of the <code>sql:execute</code> function:
+
<syntaxhighlight lang="xquery">
 +
let $id := sql:connect("jdbc:postgresql://localhost:5432/coffeehouse")
 +
return sql:execute($id, "SELECT * FROM coffees WHERE price < 10")
 +
</syntaxhighlight>
  
<code><b>sql:execute</b>($prepared-statement as xs:int, $parameters as xs:element) as element()*</code>
+
The result may look like:
  
The parameter <code>$prepared-statement</code> is the handle to the prepared statement created via <code>sql:prepare</code>. <code>$parameters</code> specifies the parameters of the prepared statement and must contain exactly the same number of <code><parameter/></code> elements as the number of placeholders in the statement. It shall follow the schema:
+
<syntaxhighlight lang="xml">
 +
<sql:row xmlns:sql="http://basex.org/modules/sql">
 +
  <sql:column name="cof_name">French_Roast</sql:column>
 +
  <sql:column name="sup_id">49</sql:column>
 +
  <sql:column name="price">3.5</sql:column>
 +
  <sql:column name="sales">15</sql:column>
 +
  <sql:column name="total">30</sql:column>
 +
</sql:row>
 +
<sql:row xmlns:sql="http://basex.org/modules/sql">
 +
  <sql:column name="cof_name">French_Roast_Decaf</sql:column>
 +
  <sql:column name="sup_id">49</sql:column>
 +
  <sql:column name="price">7.5</sql:column>
 +
  <sql:column name="sales">10</sql:column>
 +
  <sql:column name="total">14</sql:column>
 +
</sql:row>
 +
</syntaxhighlight>
  
<pre class="brush:xml">
+
==Prepared Statements==
element sql:parameters {
 
element sql:parameter {
 
  attribute type { "int"|"string"|"boolean"|"date"|"double"|"float"|"short"|"time"|"timestamp" },
 
  attribute null { "true"|"false" }?,
 
  text }+ }?
 
</pre>
 
  
For example, a prepared select statement can be executed in the following way:
+
A prepared select statement can be executed in the following way:
  
<pre class="brush:xml">
+
<syntaxhighlight lang="xquery">
 +
(: Establish a connection :)
 
let $conn := sql:connect("jdbc:postgresql://localhost:5432/coffeehouse")
 
let $conn := sql:connect("jdbc:postgresql://localhost:5432/coffeehouse")
 +
(: Obtain a handle to a prepared statement :)
 
let $prep := sql:prepare($conn, "SELECT * FROM coffees WHERE price < ? AND cof_name = ?")
 
let $prep := sql:prepare($conn, "SELECT * FROM coffees WHERE price < ? AND cof_name = ?")
 +
(: Values and types of prepared statement parameters :)
 
let $params := <sql:parameters>
 
let $params := <sql:parameters>
                <sql:parameter type='double'>10</sql:parameter>
+
                <sql:parameter type='double'>10</sql:parameter>
                <sql:parameter type='string'>French_Roast</sql:parameter>
+
                <sql:parameter type='string'>French_Roast</sql:parameter>
 
               </sql:parameters>
 
               </sql:parameters>
return sql:execute($prep, $params)
+
(: Execute prepared statement :)
</pre>
+
return sql:execute-prepared($prep, $params)
 +
</syntaxhighlight>
 +
 
 +
==SQLite==
 +
 
 +
The following expression demonstrates how SQLite can be addressed with the [https://bitbucket.org/xerial/sqlite-jdbc/ Xerial SQLite JDBC driver]:
 +
 
 +
<syntaxhighlight lang="xquery">
 +
(: Initialize driver :)
 +
sql:init("org.sqlite.JDBC"),
 +
(: Establish a connection :)
 +
let $conn := sql:connect("jdbc:sqlite:database.db")
 +
return (
 +
  (: Create a new table :)
 +
  sql:execute($conn, "drop table if exists person"),
 +
  sql:execute($conn, "create table person (id integer, name string)"),
 +
  (: Run 10 updates :)
 +
  for $i in 1 to 10
 +
  let $q := "insert into person values(" || $i || ", '" || $i || "')"
 +
  return sql:execute($conn, $q),
 +
  (: Return table contents :)
 +
  sql:execute($conn, "select * from person")
 +
)
 +
</syntaxhighlight>
  
If a the value of a field has to be set to <code>NULL</code>, then the attribute <code>null</code> of the element <code><sql:parameter/></code> has to be <code>true</code>.
+
=Errors=
  
==Commit==
+
{| class="wikitable" width="100%"
Changes made to a relational database using a connection handle <code>$connection</code> can be committed using the function <code>sql:commit</code>:
+
! width="110"|Code
 +
|Description
 +
|- valign="top"
 +
|{{Code|attribute}}
 +
|An attribute different from {{Code|type}} and {{Code|null}} is set for a {{Code|&lt;sql:parameter/&gt;}} element.
 +
|- valign="top"
 +
|{{Code|error}}
 +
|An SQL exception occurred.
 +
|- valign="top"
 +
|{{Code|id}}
 +
|A connection does not exist.
 +
|- valign="top"
 +
|{{Code|init}}
 +
|A database driver is not found.
 +
|- valign="top"
 +
|{{Code|parameters}}
 +
|No parameter type specified.
 +
|- valign="top"
 +
|{{Code|timeout}}
 +
|Query execution exceeded timeout.
 +
|- valign="top"
 +
|{{Code|type}}
 +
|The value of a parameter cannot be converted to the specified format.
 +
|}
  
<code><b>sql:commit</b>($connection as xs:int)</code>
+
=Changelog=
  
==Rollback==
+
;Version 9.6
Changes made to a relational database using a connection handle <code>$connection</code> can be rolled back using the function <code>sql:rollback</code>:
+
* Updated: {{Function||sql:execute-prepared}}, additional types added
  
<code><b>sql:rollback</b>($connection as xs:int)</code>
+
;Version 9.0
 +
* Updated: {{Function||sql:execute}}, {{Function||sql:execute-prepared}}: Return update count for updating statements. {{Code|$options}} argument added.
 +
* Updated: Connection ids are URIs now.
 +
* Updated: error codes updated; errors now use the module namespace
  
==Closing a connection==
+
;Version 7.5
A connection to a relational database can be closed using the <code>sql:close</code> function:
+
* Updated: prepared statements are now executed via {{Function||sql:execute-prepared}}
  
<code><b>sql:close</b>($connection as xs:int)</code>
+
The module was introduced with Version 7.0.

Latest revision as of 14:19, 20 July 2022

This XQuery Module contains functions to access relational databases from XQuery using SQL. With this module, you can execute query, update and prepared statements, and the result sets are returned as sequences of XML elements representing tuples. Each element has children representing the columns returned by the SQL statement.

This module uses JDBC to connect to a SQL server. Hence, your JDBC driver will need to be added to the classpath, too. If you work with the full distributions of BaseX, you can copy the driver into the lib directory. To connect to MySQL, for example, download the Connector/J Driver and extract the archive into this directory.

Conventions[edit]

All functions and errors in this module are assigned to the http://basex.org/modules/sql namespace, which is statically bound to the sql prefix.

Functions[edit]

sql:init[edit]

Signatures sql:init($class as xs:string) as empty-sequence()
Summary This function initializes a JDBC driver specified via $class. This step might be superfluous if the SQL database is not embedded.
Errors init: the specified driver is not found.

sql:connect[edit]

Signatures sql:connect($url as xs:string) as xs:anyURI
sql:connect($url as xs:string, $user as xs:string, $password as xs:string) as xs:anyURI
sql:connect($url as xs:string, $user as xs:string, $password as xs:string, $options as map(*)?) as xs:anyURI
Summary This function establishes a connection to a relational database and returns a connection id. The parameter $url is the URL of the database and shall be of the form: jdbc:<driver name>:<server> [/<database>]. If the parameters $user and $password are specified, they are used as credentials for connecting to the database. The $options parameter can be used to set connection options.
Errors error: an SQL exception occurred when connecting to the database.
Examples Connects to an SQL Server and sets autocommit to true:
sql:connect('dbc:sqlserver://DBServer', map { 'autocommit': true() })

sql:execute[edit]

Signatures sql:execute($id as xs:anyURI, $statement as xs:string) as item()*
sql:execute($id as xs:anyURI, $statement as xs:string, $options as map(*)?) as item()*
Summary This function executes an SQL $statement, using the connection with the specified $id. The returned result depends on the kind of statement:
  • If an update statement was executed, the number of updated rows will be returned as integer.
  • Otherwise, an XML representation of all results will be returned.

With $options, the following parameter can be set:

  • timeout: query execution will be interrupted after the specified number of seconds.
Errors error: an error occurred while executing SQL.
id: the specified connection does not exist.
timeout: query execution exceeded timeout.

sql:execute-prepared[edit]

Signatures sql:execute-prepared($id as xs:anyURI, $params as element(sql:parameters)) as item()*
sql:execute-prepared($id as xs:anyURI, $params as element(sql:parameters), $options as map(*)?) as item()*
Summary This function executes a prepared statement with the specified $id. The output format is identical to sql:execute. The optional parameter $params is an element <sql:parameters/> representing the parameters for a prepared statement along with their types and values. The following schema shall be used:
element sql:parameters {
  element sql:parameter {
    attribute type { "bigdecimal" | "boolean" | "byte" | "date" | "double" | "float" |
      "int" | "long" | "short" | "sqlxml" | "string" | "time" | "timestamp" },
    attribute null { "true" | "false" }?,
    text
  }+
}?

With $options, the following parameter can be set:

  • timeout: query execution will be interrupted after the specified number of seconds.
Errors attribute: an attribute different from type and null is set for a <sql:parameter/> element.
error: an error occurred while executing SQL.
id: the specified connection does not exist.
parameters: no parameter type specified.
timeout: query execution exceeded timeout.
type: the value of a parameter cannot be converted to the specified format.

sql:prepare[edit]

Signatures sql:prepare($id as xs:anyURI, $statement as xs:string) as xs:anyURI
Summary This function prepares an SQL $statement, using the specified connection $id, and returns the id reference to this statement. The statement is a string with one or more '?' placeholders. If the value of a field has to be set to NULL, then the attribute null of the <sql:parameter/> element must be true.
Errors error: an error occurred while executing SQL.
id: the specified connection does not exist.

sql:commit[edit]

Signatures sql:commit($id as xs:anyURI) as empty-sequence()
Summary This function commits the changes made to a relational database, using the specified connection $id.
Errors error: an error occurred while executing SQL.
id: the specified connection does not exist.

sql:rollback[edit]

Signatures sql:rollback($id as xs:anyURI) as empty-sequence()
Summary This function rolls back the changes made to a relational database, using the specified connection $id.
Errors error: an error occurred while executing SQL.
id: the specified connection does not exist.

sql:close[edit]

Signatures sql:close($id as xs:anyURI) as empty-sequence()
Summary This function closes a database connection with the specified $id.
Opened connections will automatically be closed after the XQuery expression has been evaluated, but in order to save memory, it is always recommendable to close connections that are not used anymore.
Errors error: an error occurred while executing SQL.
id: the specified connection does not exist.

Examples[edit]

Direct queries[edit]

A simple select statement can be executed as follows:

let $id := sql:connect("jdbc:postgresql://localhost:5432/coffeehouse")
return sql:execute($id, "SELECT * FROM coffees WHERE price < 10")

The result may look like:

<sql:row xmlns:sql="http://basex.org/modules/sql">
  <sql:column name="cof_name">French_Roast</sql:column>
  <sql:column name="sup_id">49</sql:column>
  <sql:column name="price">3.5</sql:column>
  <sql:column name="sales">15</sql:column>
  <sql:column name="total">30</sql:column>
</sql:row>
<sql:row xmlns:sql="http://basex.org/modules/sql">
  <sql:column name="cof_name">French_Roast_Decaf</sql:column>
  <sql:column name="sup_id">49</sql:column>
  <sql:column name="price">7.5</sql:column>
  <sql:column name="sales">10</sql:column>
  <sql:column name="total">14</sql:column>
</sql:row>

Prepared Statements[edit]

A prepared select statement can be executed in the following way:

(: Establish a connection :)
let $conn := sql:connect("jdbc:postgresql://localhost:5432/coffeehouse")
(: Obtain a handle to a prepared statement :)
let $prep := sql:prepare($conn, "SELECT * FROM coffees WHERE price < ? AND cof_name = ?")
(: Values and types of prepared statement parameters :)
let $params := <sql:parameters>
                 <sql:parameter type='double'>10</sql:parameter>
                 <sql:parameter type='string'>French_Roast</sql:parameter>
               </sql:parameters>
(: Execute prepared statement :)
return sql:execute-prepared($prep, $params)

SQLite[edit]

The following expression demonstrates how SQLite can be addressed with the Xerial SQLite JDBC driver:

(: Initialize driver :)
sql:init("org.sqlite.JDBC"),
(: Establish a connection :)
let $conn := sql:connect("jdbc:sqlite:database.db")
return (
  (: Create a new table :)
  sql:execute($conn, "drop table if exists person"),
  sql:execute($conn, "create table person (id integer, name string)"),
  (: Run 10 updates :)
  for $i in 1 to 10
  let $q := "insert into person values(" || $i || ", '" || $i || "')"
  return sql:execute($conn, $q),
  (: Return table contents :)
  sql:execute($conn, "select * from person")
)

Errors[edit]

Code Description
attribute An attribute different from type and null is set for a <sql:parameter/> element.
error An SQL exception occurred.
id A connection does not exist.
init A database driver is not found.
parameters No parameter type specified.
timeout Query execution exceeded timeout.
type The value of a parameter cannot be converted to the specified format.

Changelog[edit]

Version 9.6
Version 9.0
  • Updated: sql:execute, sql:execute-prepared: Return update count for updating statements. $options argument added.
  • Updated: Connection ids are URIs now.
  • Updated: error codes updated; errors now use the module namespace
Version 7.5

The module was introduced with Version 7.0.