如何使用BaseX和XQuery查询SQLITE数据库? [英] How to query an SQLITE database using BaseX and XQuery?

查看:119
本文介绍了如何使用BaseX和XQuery查询SQLITE数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是否需要为SQLite安装驱动程序,或者查询是否有问题?

Do I need to install a driver for SQLite, or is it a problem with the query, perhaps?

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 $ 

示例查询:

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

我还没有尝试过与PostgreSQL或MySQL.

I haven't tried with PostgreSQL or MySQL yet.

推荐答案

是的,您需要安装与数据库后端匹配的驱动程序.

Yes, you need to install the driver, that matches the database backend, you use.

(非常好)文档到BaseX状态,在:

The (very good) documentation to BaseX states, in the section "SQL Module":

此模块使用JDBC连接到SQL Server.因此,您的JDBC驱动程序也需要添加到类路径中.如果使用BaseX的完整发行版,则可以将驱动程序复制到lib目录中.例如,要连接到MySQL,请下载Connector/J驱动程序,然后将归档文件解压缩到该目录中.

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/目录中),即可开始运行.

For SQLite this could be xerial/sqlite-jdbc. Adding this JAR to your classpath (or to the lib/ directory in BaseX installation directory) should get you up running.

似乎您的语法可能是错误的,因为您的示例缺少驱动程序初始化的任何提示,以及使用的连接字符串,与

It also seems, that your syntax may be wrong, as your example lacks any hint of initialization of the driver, as well as using a connection string, that is different to the one, mentioned in the BaseX documentation. But I don't know SQL well enough, so there may be valid variants to those. Testing the documentation's example:

(: 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>

这篇关于如何使用BaseX和XQuery查询SQLITE数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆