数据库查询语言(核心和SQLite后端)
项目描述
HTSQL是一个针对关系数据库的综合导航查询语言。HTSQL是为数据分析师和其他意外程序员设计的,他们需要解决复杂的业务查询,并需要一个高效的工具来编写和共享数据库查询。HTSQL是免费和开源软件。更多信息,请访问http://htsql.org/。
此软件包提供HTSQL核心和SQLite后端。
安装说明
要使用pip包管理器安装HTSQL,请运行
# pip install HTSQL
HTSQL与SQLite数据库配合使用无需额外设置。要在其他数据库服务器上运行HTSQL,您需要安装额外的数据库后端。
要安装 PostgreSQL 后端,运行
# pip install HTSQL-PGSQL
要安装 MySQL 后端,运行
# pip install HTSQL-MYSQL
要安装 Oracle 后端,运行
# pip install HTSQL-ORACLE
要安装 Microsoft SQL Server 的后端,运行
# pip install HTSQL-MSSQL
或者,您可以从 http://htsql.org/download/ 下载各种 Linux 发行版的二进制包。
快速入门
要验证 HTSQL 是否正确安装,运行
$ htsql-ctl --version
您可以在任何关系型数据库上使用 HTSQL。在我们的示例中,我们使用 HTSQL 示例数据库,可以从 http://dist.htsql.org/misc/htsql_demo.sqlite 下载。
要启动一个命令行 shell,在其中您可以输入并执行 HTSQL 查询,运行
$ htsql-ctl shell sqlite:htsql_demo.sqlite Type 'help' for more information, 'exit' to quit the shell.
sqlite:htsql_demo.sqlite 参数是一个数据库连接 URI,其一般形式为
<engine>://<user>:<pass>@<host>:<port>/<database>
例如,以下都是有效的连接 URI
sqlite:htsql_demo.sqlite pgsql:///htsql_demo mysql://root@localhost:3306/htsql_demo
使用 --password 选项让 htsql-ctl 要求您输入密码。
在 shell 中,您可以输入并执行 HTSQL 查询
htsql_demo$ /school | school | +-----------------------------------------------+ | code | name | campus | -+------+-------------------------------+--------+- | art | School of Art & Design | old | | bus | School of Business | south | | edu | College of Education | old | ...
htsql-ctl 脚本还提供了一个内置的 Web 服务器。可以按如下方式启动
$ htsql-ctl serve sqlite:htsql_demo.sqlite Starting an HTSQL server on localhost:8080 over htsql_demo.sqlite
然后您可以使用浏览器或其他 HTTP 用户代理访问 HTSQL。
有关使用和配置 HTSQL 的更多信息,请参阅 http://htsql.org/doc/handbook.html。
从 Python 使用 HTSQL
创建 HTSQL 实例
>>> from htsql import HTSQL >>> demo = HTSQL("sqlite:htsql_demo")
使用该实例执行 HTSQL 查询。例如,为了找到所有符合给定模式的学校记录,编写
>>> query = "/school?name~$pattern" >>> for row in demo.produce(query, pattern='art'): ... print row ... school(code=u'art', name=u'School of Art & Design', campus=u'old') school(code=u'la', name=u'School of Arts and Humanities', campus=u'old')
在下一个示例中,对于旧校园中的所有学校,我们获取相关课程和系部的数量
>>> query = "/school{name, count(program), count(department)}?campus='old'" >>> for row in demo.produce(query): ... print "%s: %d programs, %d departments" % row ... School of Art & Design: 3 programs, 2 departments College of Education: 7 programs, 2 departments School of Arts and Humanities: 9 programs, 5 departments School of Natural Sciences: 6 programs, 4 departments
有关使用 Python 配合 HTSQL 的详细说明,请参阅 http://htsql.org/doc/embed.html。
致谢
HTSQL 版权所有 Prometheus Research, LLC。HTSQL 由 Clark C. Evans <cce@clarkevans.com> 和 Kirill Simonov <xi@resolvent.net> 编写。
Simons 基金会对 HTSQL 提供了慷慨的支持。这项材料也基于美国国家科学基金会(Grant #0944460)资助的工作。
项目详情
下载文件
下载适用于您的平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。