SQL数据库的通用命令行工具-usql
2018-06-18 07:03:28 阿炯

本站赞助商链接,请多关照。 usql 是 SQL 数据库的通用命令行界面。支持主流的数据库软件,如 PostgreSQL, MySQL, Oracle Database, SQLite3, Microsoft SQL Server 以及许多其他的数据库(包括 NoSQL 和非关系型数据库)。采用Go语言开发并在MIT协议下授权。


usql provides a simple way to work with SQL and NoSQL databases via a command-line inspired by PostgreSQL's psql. usql supports most of the core psql features, such as variables, backticks, and commands and has additional features that psql does not, such as syntax highlighting, context-based completion, and multiple database support.

Database administrators and developers that would prefer to work with a tool like psql with non-PostgreSQL databases, will find usql intuitive, easy-to-use, and a great replacement for the command-line clients/tools for other databases.

usql 的灵感来自 PostgreSQL 的 psql(通过其help输出可以看到与psql非常相似),通过命令行提供了一种简单的方式来操作 SQL 和 NoSQL 数据库。usql 支持大多数 psql 的核心特性,如变量、反引号(backticks)和命令,并具有 psql 不支持的其他功能,如语法高亮、基于上下文的自动补全和多数据库支持等。

使用方法

安装完成后,usql 可以像以下方式这样使用:
# connect to a postgres database
$ usql pg://user:pass@host/dbname
$ usql pgsql://user:pass@host/dbname
$ usql postgres://user:pass@host:port/dbname
$ usql pg://
$ usql /var/run/postgresql

# connect to a mysql database
$ usql my://user:pass@host/dbname
$ usql mysql://user:pass@host:port/dbname
$ usql my://
$ usql /var/run/mysqld/mysqld.sock

# connect to a mssql (Microsoft SQL) database
$ usql ms://user:pass@host/dbname
$ usql ms://user:pass@host/instancename/dbname
$ usql mssql://user:pass@host:port/dbname
$ usql ms://

# connect to a mssql (Microsoft SQL) database using Windows domain authentication
$ runas /user:ACME\wiley /netonly "usql mssql://host/dbname/"

# connect to a oracle database
$ usql or://user:pass@host/sid
$ usql oracle://user:pass@host:port/sid
$ usql or://

# connect to a cassandra database
$ usql ca://user:pass@host/keyspace
$ usql cassandra://host/keyspace
$ usql cql://host/
$ usql ca://

# connect to a sqlite database that exists on disk
$ usql dbname.sqlite3

# NOTE: when connecting to a SQLite database, if the "<driver>://" or
# "<driver>:" scheme/alias is omitted, the file must already exist on disk.
#
# if the file does not yet exist, the URL must incorporate file:, sq:, sqlite3:,
# or any other recognized sqlite3 driver alias to force usql to create a new,
# empty database at the specified path:
$ usql sq://path/to/dbname.sqlite3
$ usql sqlite3://path/to/dbname.sqlite3
$ usql file:/path/to/dbname.sqlite3

命令行选项

$ usql --help
usql, the universal command-line interface for SQL databases.

usql 0.7.0
Usage: usql [--command COMMAND] [--file FILE] [--output OUTPUT] [--username USERNAME] [--password] [--no-password] [--no-rc] [--single-transaction] [--set SET] DSN

Positional arguments:
DSN    database url

Options:
--command COMMAND, -c COMMAND
    run only single command (SQL or internal) and exit
--file FILE, -f FILE   execute commands from file and exit
--output OUTPUT, -o OUTPUT
    output file
--username USERNAME, -U USERNAME
    database user name [default: ken]
--password, -W         force password prompt (should happen automatically)
--no-password, -w      never prompt for password
--no-rc, -X            do not read start up file
--single-transaction, -1
    execute as a single transaction (if non-interactive)
--set SET, -v SET      set variable NAME=VALUE
--help, -h             display this help and exit
--version              display version and exit


最新版本:0.7
支持 Cassandra(CQL)
修复语法高亮显示
新增 -v / --set 命令行参数(兼容 psql)
新增数据库 “批量查询”功能(ql + cql)
修复查询前缀检测/解析的问题
更改依赖管理(从 dep 切换到 vgo)

项目主页:https://github.com/xo/usql