数据库查询语言(core&sqlite后端)
HTSQL的Python项目详细描述
htsql是一种关系型的综合导航查询语言。 数据库。htsql是为数据分析员和其他意外的设计的 需要解决复杂业务查询并需要 用于编写和共享数据库查询的高效工具。htsql是^{em1}免费的 以及开源软件。有关详细信息,请访问http://htsql.org/。
这个包提供htsql核心和sqlite后端。
安装说明
要使用pippackage manager安装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://localhost/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由普罗米修斯研究有限责任公司版权所有。HTSQL由 克拉克·C·埃文斯和基里尔·西蒙诺夫。
西蒙斯基金会对htsql提供了慷慨的支持。 这份材料也是基于国家支持的工作 科学基金会拨款0944460.