<feed xmlns="http://www.w3.org/2005/Atom">
    <id>urn:uuid:1feaa84c-9f0c-49ba-87ad-a4d807e80b91</id>
    <updated>2007-12-01T18:54:38+00:00</updated>
    <title>Querying SQL Databases from XQuery (SQLModule)</title>
    <author>
        <atom:name xmlns:atom="http://www.w3.org/2005/Atom">wolf</atom:name>
    </author>
    <atom:category xmlns:atom="http://www.w3.org/2005/Atom" scheme="http://exist-db.org/NS/wiki/type/" term="wiki"/>
    <link href="#" rel="edit" type="application/atom+xml"/>
    <link href="#" rel="self" type="application/atom+xml"/>
    <atom:entry xmlns:atom="http://www.w3.org/2005/Atom">
        <atom:id>urn:uuid:ec38137e-08ab-4f0e-9823-907b4cc4048c</atom:id>
        <wiki:id xmlns:wiki="http://exist-db.org/xquery/wiki">QueryingSQLDatabases</wiki:id>
        <wiki:editor xmlns:wiki="http://exist-db.org/xquery/wiki">wiki</wiki:editor>
        <wiki:is-index xmlns:wiki="http://exist-db.org/xquery/wiki">false</wiki:is-index>
        <atom:published>2007-12-01T18:54:24+00:00</atom:published>
        <atom:updated>2012-04-09T16:21:50.905+02:00</atom:updated>
        <atom:author>
            <atom:name>wolf</atom:name>
        </atom:author>
        <atom:title>Querying SQL Databases from XQuery (SQLModule)</atom:title>
        <atom:content type="xhtml">
            <article xmlns="http://www.w3.org/1999/xhtml">
                <p>This howto explains how you may query or update SQL databases from XQuery returning the results as XML nodesets. This tutorial makes use of the sql extension module for eXist; this is available in eXist from 2006-09-25.</p>
                <section>
                    <section>
                        <h2>1.1 eXist Configuration</h2>
                        <p>Firstly you need to configure eXist to load the additional module, you will need to add the following to the xquery/builtin-modules node of conf.xml (which can be found in EXIST_HOME -</p>
                        <div class="ext:code?lang=xml">
&lt;module class="org.exist.xquery.modules.sql.SQLModule"
       uri="http://exist-db.org/xquery/sql" /&gt;
</div>
                        <p>NB - eXist will need to be restarted for this change to take effect.</p>
                        <section>
                            <h2>1.2 JDBC Drivers</h2>
                            <p>The SQL Module uses JDBC for its database connectivity and as such for each database type that you wish to connect to a JDBC Driver is required. JDBC Drivers should be placed in EXIST_HOME/lib/user.</p>
                            <section>
                                <h2>1.3 The SQL module</h2>
                                <p>The SQL module provides two main functions - get-connection() and execute().</p>
                                <section>
                                    <h3>1.3.1 get-connection()</h3>
                                    <p>Used for opening a connection to the database. The connection persists for the lifetime of the executing query. There are two implementations -</p>
                                    <div class="ext:code?lang=xquery">
get-connection($jdbcClass, $jdbcConnection)

get-connection($jdbcClass, $jdbcConnection, $dbUser, $dbPassword)
</div>
                                    <ul>
                                        <li>
                                            <span class="strong">jdbcClass</span> is the JDBC Driver Class, e.g. for MySQL this would be "com.mysql.jdbc.Driver"</li>
                                    </ul>
                                    <ul>
                                        <li>
                                            <span class="strong">jdbcConnection</span> is the JDBC Connection String. e.g. for a MySQL server running on the local machine with a database called "pies" this would be - "  "</li>
                                    </ul>
                                    <ul>
                                        <li>
                                            <span class="strong">dbUser</span> is the database/schema user's username</li>
                                    </ul>
                                    <ul>
                                        <li>
                                            <span class="strong">dbPassword</span> is the password for the database/schema user</li>
                                    </ul>
                                    <p>The get-connection() function returns an    which is the id of the open database connection.</p>
                                </section>
                            </section>
                            <section>
                                <h2>1.3.2 execute()</h2>
                                <p>Executes either a query or update against the database. The implementation is -</p>
                                <div class="ext:code?lang=xquery">
execute($connection, $sql, $useColumnNames)
</div>
                                <ul>
                                    <li>
                                        <span class="strong">connection</span> is the connection id obtained from get-connection()</li>
                                </ul>
                                <ul>
                                    <li>
                                        <span class="strong">sql</span> is the SQL statement to execute</li>
                                </ul>
                                <ul>
                                    <li>
                                        <span class="strong">useColumnNames</span> is an    value indicating whether the resultant XML should use the Column Names as the node names.</li>
                                </ul>
                                <p>The execute() function returns a Node representing the SQL results. If the SQL query was an update then an update count is returned.</p>
                                <section>
                                    <h2>1.4 Example: Querying a SQL Database</h2>
                                    <div class="ext:code?lang=xquery">
xquery version "1.0";

declare namespace sql="http://exist-db.org/xquery/sql";

let $connection := sql:get-connection("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/pies", "root", "") return

sql:execute($connection, "select * from pieFillings;", fn:true())
</div>
                                    <section>
                                        <h2>1.5 Example: Updating a SQL Database</h2>
                                        <div class="ext:code?lang=xquery">
xquery version "1.0";

declare namespace sql="http://exist-db.org/xquery/sql";

let $connection := sql:get-connection("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/pies", "root", "") return

sql:execute($connection, "insert into pieFillings (filling, cost) values ('apple', 1.0);", fn:false())
</div>
                                    </section>
                                </section>
                            </section>
                        </section>
                    </section>
                </section>
            </article>
        </atom:content>
        <atom:link type="blog" href="/db/apps/wiki/data/HowTo/SQLDatabases"/>
    </atom:entry>
</feed>