如何使用 BaseX 和 XQuery 查询 SQLITE 数据库?
How to query an SQLITE database using BaseX and XQuery?
我是否需要为 SQLite 安装驱动程序,或者查询是否有问题?
nicholas $
nicholas $ basex sqlite.xq
Stopped at /home/nicholas/xquery/sqlite.xq, 1/23:
[sql:error] An SQL exception occurred: No suitable driver found for jdbc:sqlite3://localhost:5432/home/nicholas/.local/share/liferea/liferea.db
nicholas $
nicholas $ cat sqlite.xq
let $id := sql:connect("jdbc:sqlite3://localhost:5432/home/nicholas/.local/share/liferea/liferea.db")
return sql:execute($id, "SELECT title, description FROM items LIMIT 3;")
nicholas $
example查询:
let $id := sql:connect("jdbc:postgresql://localhost:5432/coffeehouse")
return sql:execute($id, "SELECT * FROM coffees WHERE price < 10")
我还没有 tried 使用 PostgreSQL 或 MySQL。
是的,您需要安装与您使用的数据库后端匹配的驱动程序。
(很好)documentation to BaseX states, in the section "SQL Module":
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.
对于 SQLite 这可能是 xerial/sqlite-jdbc。将此 JAR 添加到您的 classpath(或 BaseX 安装目录中的 lib/
目录) 应该让你起来 运行.
看来,您的语法可能有误,因为您的示例缺少驱动程序初始化的任何提示,也没有使用与 mentioned 中的连接字符串不同的连接字符串BaseX 文档。但我对 SQL 的了解还不够,因此可能存在有效的变体。测试文档的示例:
(: 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")
)
我得到了记录的结果:
0
0
1
1
1
1
1
1
1
1
1
1
<sql:row xmlns:sql="http://basex.org/modules/sql">
<sql:column name="id">1</sql:column>
<sql:column name="name">1</sql:column>
</sql:row>
<sql:row xmlns:sql="http://basex.org/modules/sql">
<sql:column name="id">2</sql:column>
<sql:column name="name">2</sql:column>
</sql:row>
<sql:row xmlns:sql="http://basex.org/modules/sql">
<sql:column name="id">3</sql:column>
<sql:column name="name">3</sql:column>
</sql:row>
<sql:row xmlns:sql="http://basex.org/modules/sql">
<sql:column name="id">4</sql:column>
<sql:column name="name">4</sql:column>
</sql:row>
<sql:row xmlns:sql="http://basex.org/modules/sql">
<sql:column name="id">5</sql:column>
<sql:column name="name">5</sql:column>
</sql:row>
<sql:row xmlns:sql="http://basex.org/modules/sql">
<sql:column name="id">6</sql:column>
<sql:column name="name">6</sql:column>
</sql:row>
<sql:row xmlns:sql="http://basex.org/modules/sql">
<sql:column name="id">7</sql:column>
<sql:column name="name">7</sql:column>
</sql:row>
<sql:row xmlns:sql="http://basex.org/modules/sql">
<sql:column name="id">8</sql:column>
<sql:column name="name">8</sql:column>
</sql:row>
<sql:row xmlns:sql="http://basex.org/modules/sql">
<sql:column name="id">9</sql:column>
<sql:column name="name">9</sql:column>
</sql:row>
<sql:row xmlns:sql="http://basex.org/modules/sql">
<sql:column name="id">10</sql:column>
<sql:column name="name">10</sql:column>
</sql:row>
我是否需要为 SQLite 安装驱动程序,或者查询是否有问题?
nicholas $
nicholas $ basex sqlite.xq
Stopped at /home/nicholas/xquery/sqlite.xq, 1/23:
[sql:error] An SQL exception occurred: No suitable driver found for jdbc:sqlite3://localhost:5432/home/nicholas/.local/share/liferea/liferea.db
nicholas $
nicholas $ cat sqlite.xq
let $id := sql:connect("jdbc:sqlite3://localhost:5432/home/nicholas/.local/share/liferea/liferea.db")
return sql:execute($id, "SELECT title, description FROM items LIMIT 3;")
nicholas $
example查询:
let $id := sql:connect("jdbc:postgresql://localhost:5432/coffeehouse")
return sql:execute($id, "SELECT * FROM coffees WHERE price < 10")
我还没有 tried 使用 PostgreSQL 或 MySQL。
是的,您需要安装与您使用的数据库后端匹配的驱动程序。
(很好)documentation to BaseX states, in the section "SQL Module":
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.
对于 SQLite 这可能是 xerial/sqlite-jdbc。将此 JAR 添加到您的 classpath(或 BaseX 安装目录中的 lib/
目录) 应该让你起来 运行.
看来,您的语法可能有误,因为您的示例缺少驱动程序初始化的任何提示,也没有使用与 mentioned 中的连接字符串不同的连接字符串BaseX 文档。但我对 SQL 的了解还不够,因此可能存在有效的变体。测试文档的示例:
(: 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")
)
我得到了记录的结果:
0
0
1
1
1
1
1
1
1
1
1
1
<sql:row xmlns:sql="http://basex.org/modules/sql">
<sql:column name="id">1</sql:column>
<sql:column name="name">1</sql:column>
</sql:row>
<sql:row xmlns:sql="http://basex.org/modules/sql">
<sql:column name="id">2</sql:column>
<sql:column name="name">2</sql:column>
</sql:row>
<sql:row xmlns:sql="http://basex.org/modules/sql">
<sql:column name="id">3</sql:column>
<sql:column name="name">3</sql:column>
</sql:row>
<sql:row xmlns:sql="http://basex.org/modules/sql">
<sql:column name="id">4</sql:column>
<sql:column name="name">4</sql:column>
</sql:row>
<sql:row xmlns:sql="http://basex.org/modules/sql">
<sql:column name="id">5</sql:column>
<sql:column name="name">5</sql:column>
</sql:row>
<sql:row xmlns:sql="http://basex.org/modules/sql">
<sql:column name="id">6</sql:column>
<sql:column name="name">6</sql:column>
</sql:row>
<sql:row xmlns:sql="http://basex.org/modules/sql">
<sql:column name="id">7</sql:column>
<sql:column name="name">7</sql:column>
</sql:row>
<sql:row xmlns:sql="http://basex.org/modules/sql">
<sql:column name="id">8</sql:column>
<sql:column name="name">8</sql:column>
</sql:row>
<sql:row xmlns:sql="http://basex.org/modules/sql">
<sql:column name="id">9</sql:column>
<sql:column name="name">9</sql:column>
</sql:row>
<sql:row xmlns:sql="http://basex.org/modules/sql">
<sql:column name="id">10</sql:column>
<sql:column name="name">10</sql:column>
</sql:row>