推薦一款支持 SQL/NoSQL 數據庫的通用命令行工具 USQL

USQL 是一個使用 Go 語言開發的支持 SQL/NoSQL 數據庫的通用命令行工具,支持多種主流的數據庫軟件。好比:PostgreSQL、MySQL、Oracle Database、SQLite三、Microsoft SQL Server 以及許多其它的數據庫(包括 NoSQL 和非關係型數據庫)。mysql

USQL 的靈感來自 PostgreSQL 的 PSQL,USQL 支持大多數 PSQL 的核心特性,好比:設置變量、反引號參數。並具備 PSQL 不支持的其它功能,如語法高亮、基於上下文的自動補全和多數據庫支持等。linux

項目地址:https://github.com/xo/usqlgit

安裝 USQL

因爲 USQL 使用 Go 語言開發,具有了良好的跨平臺特性。USQL 安裝很是簡單,官方也提供二進制、Homebrew、Scoop等多種安裝方式。這裏咱們就使用最具通用性的二進制方式安裝,以 Linux 平臺爲例:github

$ 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複製代碼

若是你使用其它平臺,可根據實際狀況在官方下載頁面下載對應版本。sql

USQL 用法

  • USQL 命令行語法shell

$ 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複製代碼
  • 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複製代碼
  • USQL 目前支持的數據庫類型bash

Database (scheme/driver) Protocol Aliases [real driver]
Microsoft SQL Server (mssql) ms, sqlserver
MySQL (mysql) my, mariadb, maria, percona, aurora
Oracle (ora) or, oracle, oci8, oci
PostgreSQL (postgres) pg, postgresql, pgsql
SQLite3 (sqlite3) sq, sqlite, file
Amazon Redshift (redshift) rs [postgres]
CockroachDB (cockroachdb) cr, cockroach, crdb, cdb [postgres]
MemSQL (memsql) me [mysql]
TiDB (tidb) ti [mysql]
Vitess (vitess) vt [mysql]
Google Spanner (spanner) gs, google, span (not yet public)
MySQL (mymysql) zm, mymy
PostgreSQL (pgx) px
Apache Avatica (avatica) av, phoenix
Apache Ignite (ignite) ig, gridgain
Cassandra (cql) ca, cassandra, datastax, scy, scylla
ClickHouse (clickhouse) ch
Couchbase (n1ql) n1, couchbase
Cznic QL (ql) ql, cznic, cznicql
Firebird SQL (firebirdsql) fb, firebird
Microsoft ADODB (adodb) ad, ado
ODBC (odbc) od
OLE ODBC (oleodbc) oo, ole, oleodbc [adodb]
Presto (presto) pr, prestodb, prestos, prs, prestodbs
SAP HANA (hdb) sa, saphana, sap, hana
Snowflake (snowflake) sf
VoltDB (voltdb) vo, volt, vdb

USQL 使用實例

鏈接到不一樣數據庫

這裏演示幾個經常使用數據庫鏈接方法,其它數據庫也相似:oracle

  • 鏈接到一個 MySQL 數據庫app

# 使用默認信息鏈接到數據庫
$ usql my://
# 使用用戶名和密碼鏈接到指定的數據庫
$ usql my://user:pass@host/dbname
# 使用用戶名、密碼和端口鏈接到指定的數據庫
$ usql mysql://user:pass@host:port/dbname
# 使用指定的套接字鏈接數據庫
$ usql /var/run/mysqld/mysqld.sock複製代碼
  • 鏈接到一個 PostgreSQL 數據庫

# 使用默認信息鏈接到數據庫
$ usql pg://
# 使用用戶名和密碼鏈接到指定的數據庫
$ usql pg://user:pass@host/dbname
# 使用用戶名、密碼和端口鏈接到指定的數據庫
$ usql postgres://user:pass@host:port/dbname
# 使用指定的套接字鏈接數據庫
$ usql /var/run/postgresql複製代碼
  • 鏈接到一個 Oracle 數據庫

# 使用默認信息鏈接到數據庫
$ usql or://
# 使用用戶名和密碼鏈接到指定的數據庫
$ usql or://user:pass@host/sid
# 使用用戶名、密碼和端口鏈接到指定的數據庫
$ usql oracle://user:pass@host:port/sid複製代碼
  • 鏈接到一個 SQL Server 數據庫

# 使用默認信息鏈接到數據庫
$ usql ms://
# 使用用戶名和密碼鏈接到指定的數據庫
$ usql ms://user:pass@host/dbname
# 使用用戶名、密碼和端口鏈接到指定的數據庫
$ usql mssql://user:pass@host:port/dbname
# 使用用戶名和密碼鏈接到指定實例的數據庫
$ usql ms://user:pass@host/instancename/dbname複製代碼

執行查詢和命令

如下例子均在 MySQL 數據庫環境以執行。

  • 建立一個名爲 test 的數據庫,並在這個數據庫中創建的名爲 test 的表中新增一行數據。

$ usql my://root:000000@localhost
Connected with driver mysql (5.7.18-log)
Type "help" for help.

my:root@localhost=> create database test;
CREATE DATABASE 1
my:root@localhost=> use test;
USE
my:root@localhost=> CREATE TABLE IF NOT EXISTS `test`(
my:root@localhost(>    `test_id` INT,
my:root@localhost(>    `name` VARCHAR(100) NOT NULL
my:root@localhost(> )ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE
my:root@localhost=> insert into test (test_id, name) values (1, 'hello');
INSERT 1
my:root@localhost=> select * from test;
test_id | name
+---------+-------+
1 | hello
(1 rows)複製代碼
  • 鏈接到一個 MySQL 數據庫並運行一個名爲 script.sql 的腳本

$ usql my://root:000000@localhost/wordpress -f script.sql
id  | post_author |           post_date
+-----+-------------+-------------------------------+
673 |           1 | 2007-01-22 06:31:27 +0800 CST
675 |           1 | 2007-01-22 06:36:39 +0800 CST
(2 rows)複製代碼

接下來咱們再舉幾個使用內置命令進行操做的例子。

  • 打印並執行緩充區中的 SQL 語句

my:root@localhost=> select *
my:root@localhost-> from test
my:root@localhost-> \p
select *
from test
my:root@localhost-> \g
test_id | name
+---------+-------+
1 | hello
(1 rows)複製代碼
  • 快速切換到另外一個數據庫鏈接

ms:mike@192.168.100.210:1433/testdb=> \c my://root:000000@localhost/wordpress
Connected with driver mysql (5.7.18-log)
my:root@localhost/wordpress=> select * from wp_postmeta limit 5;
meta_id | post_id | meta_key | meta_value
+---------+---------+----------+------------+
1 |       1 | views    |     185245
2 |       2 | views    |       5659
3 |       3 | views    |       2197
4 |       4 | views    |       1739
5 |       5 | views    |       2002
(5 rows)複製代碼
  • 使用變量進行條件查詢

# 設置變量
my:root@localhost/wordpress=> \set meta 2179
# 查看當前已設置的變量
my:root@localhost/wordpress=> \set
meta = '2179'
# 使用變量作爲條件進行查詢
my:root@localhost/wordpress=> select * from wp_postmeta where meta_value=:'meta';
meta_id | post_id | meta_key | meta_value
+---------+---------+----------+------------+
156 |     163 | views    |       2179
203 |     211 | views    |       2179
(2 rows)複製代碼

變量調用支持 :NAME、:’NAME’、和 :」NAME」 這三種方式進行調用。

  • 使用反引號參數

my:root@localhost/wordpress=> \echo Welcome `echo $USER` -- 'currently:' "(" `date` ")"
Welcome root -- currently: ( Tue Jun 19 13:47:31 CST 2018 )複製代碼

反引號參數的執行結果也能夠直接設置爲一個變量的值:

my:root@localhost=> \set MYVAR `date "+%Y-%m-%d"`
my:root@localhost=> \echo :MYVAR
2018-06-19
my:root@localhost=> select id,post_author,post_date  from wp_posts where post_date < :'MYVAR'  limit 2;
id  | post_author |           post_date
+-----+-------------+-------------------------------+
673 |           1 | 2007-01-22 06:31:27 +0800 CST
675 |           1 | 2007-01-22 06:36:39 +0800 CST
(2 rows)複製代碼

參考文檔

http://www.google.com
https://github.com/xo/usql



相關文章
相關標籤/搜索