USQL 是一款使用 Go 语言开发的支持 SQL/NoSQL 数据库的通用命令行工具,它支持多种主流的数据库软件,目前最新版本是usql 0.7.0。比如 PostgreSQL、MySQL、Oracle Database、SQLite3、Microsoft SQL Server 以及许多其它的数据库(包括 NoSQL 和非关系型数据库)。
USQL 的灵感来自 PostgreSQL 的 PSQL,USQL 支持大多数 PSQL 的核心特性,比如:设置变量、反引号参数。并具有 PSQL 不支持的其它功能,如语法高亮、基于上下文的自动补全和多数据库支持等。
USQL项目地址
安装 USQL
由于 USQL 使用 Go 语言开发,具备了良好的Linux、Windows等跨平台特性。USQL 安装非常简单,官方也提供二进制、Homebrew、Scoop等多种安装方式。这里我们就使用最具通用性的二进制方式安装,以 Linux 平台为例:
$ wget https://github.com/xo/usql/releases/download/v0.7.0/usql-0.7.0-linux-amd64.tar.bz2
$ tar xjvf usql-0.7.0-linux-amd64.tar.bz2
$ sudo mv usql /usr/local/bin
如果你使用其它平台,可根据实际情况在官方下载页面下载对应版本。
USQL 用法
1.USQL 命令行语法
$ 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: mike]
--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
2.USQL 支持的反斜杠命令
$ usql
Type "help" for help.
(not connected)=> /?
General
/q quit usql
/copyright show usql usage and distribution terms
/drivers display information about available database drivers
/g [FILE] or ; execute query (and send results to file or |pipe)
/gexec execute query and execute each value of the result
/gset [PREFIX] execute query and store results in usql variables
Help
/? [commands] show help on backslash commands
/? options show help on usql command-line options
/? variables show help on special variables
Query Buffer
/e [FILE] [LINE] edit the query buffer (or file) with external editor
/p show the contents of the query buffer
/raw show the raw (non-interpolated) contents of the query buffer
/r reset (clear) the query buffer
/w FILE write query buffer to file
Input/Output
/echo [STRING] write string to standard output
/i FILE execute commands from file
/ir FILE as /i, but relative to location of current script
Transaction
/begin begin a transaction
/commit commit current transaction
/rollback rollback (abort) current transaction
Connection
/c URL connect to database with url
/c DRIVER PARAMS... connect to database with SQL driver and parameters
/Z close database connection
/password [USERNAME] change the password for a user
/conninfo display information about the current database connection
Operating System
/cd [DIR] change the current working directory
/setenv NAME [VALUE] set or unset environment variable
/! [COMMAND] execute command in shell or start interactive shell
Variables
/prompt [TEXT] NAME prompt user to set internal variable
/set [NAME [VALUE]] set internal variable, or list all if no parameters
/unset NAME unset (delete) internal variable
3.USQL 目前支持的数据库类型Database (scheme/driver)Protocol Aliases [real driver]