それマグで!

知識はカップより、マグでゆっくり頂きます。 takuya_1stのブログ

習慣に早くから配慮した者は、 おそらく人生の実りも大きい。

MySQL/MariaDBのSQLとテーブル名を補完する mycli が便利

mycli が便利でやばい

SELECT / INSERT などを補完してくれて、テーブル名やカラム名を補完してくれて、SQLを色付けしてくれる。

f:id:takuya_1st:20170515010905p:plain

インストー

python で出来てるのでpip でインストー

pip install mycli

pip がないときは pyenv を

pyenv から準備すれば大丈夫です。

pyenv install $VERSION

pyenv の環境作成の手順は→ここに書いた

起動

mycli

引数なしで起動すると localhost:3308 の mysql に繋いでくれる。ローカルホスト以外に接続する場合やユーザー名やパスワード指定など、その他オプションは、mysql コマンドと同じ。

設定

初回起動すると、次のようなファイルが作られる。

 ~/.myclirc

このファイルにアレコレかくだけ。色付けは を変えるだけ

 38 # Syntax coloring style. Possible values (many support the "-dark" suffix):
 39 # manni, igor, xcode, vim, autumn, vs, rrt, native, perldoc, borland, tango, emacs,
 40 # friendly, monokai, paraiso, colorful, murphy, bw, pastie, paraiso, trac, default,
 41 # fruity.
 42 # Screenshots at http://mycli.net/syntax
 43 syntax_style = native

出力形式も選べる。MySQLに従いながら、

mysql takuya@localhost:epgrec> \T
Table type  not yet implemented.  Allowed types:
        simple
        plain
        grid
        fancy_grid
        pipe
        orgtbl
        psql
        rst
        mediawiki
        html
        latex
        latex_booktabs
        tsv
Time: 0.000s

主に使えるコマンド

mysql takuya@localhost:epgrec> ?
+-------------+-------------------+---------------------------------------------------------+
| Command     | Shortcut          | Description                                             |
|-------------+-------------------+---------------------------------------------------------|
| \G          | \G                | Display results vertically.                             |
| \dt         | \dt [table]       | List or describe tables.                                |
| \e          | \e                | Edit command with editor. (uses $EDITOR)                |
| \f          | \f [name]         | List or execute favorite queries.                       |
| \fd         | \fd [name]        | Delete a favorite query.                                |
| \fs         | \fs name query    | Save a favorite query.                                  |
| \l          | \l                | List databases.                                         |
| \timing     | \t                | Toggle timing of commands.                              |
| connect     | \r                | Reconnect to the database. Optional database argument.  |
| exit        | \q                | Exit.                                                   |
| help        | \?                | Show this help.                                         |
| nopager     | \n                | Disable pager, print to stdout.                         |
| notee       | notee             | stop writing to an output file                          |
| pager       | \P [command]      | Set PAGER. Print the query results via PAGER            |
| prompt      | \R                | Change prompt format.                                   |
| quit        | \q                | Quit.                                                   |
| rehash      | \#                | Refresh auto-completions.                               |
| source      | \. filename       | Execute commands from file.                             |
| status      | \s                | Get status information from the server.                 |
| system      | system [command]  | Execute a system commmand.                              |
| tableformat | \T                | Change Table Type.                                      |
| tee         | tee [-o] filename | write to an output file (optionally overwrite using -o) |
| use         | \u                | Change to a new database.                               |
+-------------+-------------------+---------------------------------------------------------+
Time: 0.003s

mysql だけの場合

MySQLmysql コマンドの場合は、次のようになってたので、使えるコマンドがだいぶ違いますね。

mysql> ?

For information about MySQL products and services, visit:
   http://www.mysql.com/
For developer information, including the MySQL Reference Manual, visit:
   http://dev.mysql.com/
To buy MySQL Enterprise support, training, or other products, visit:
   https://shop.mysql.com/

List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
?         (\?) Synonym for `help'.
clear     (\c) Clear the current input statement.
connect   (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
edit      (\e) Edit command with $EDITOR.
ego       (\G) Send command to mysql server, display result vertically.
exit      (\q) Exit mysql. Same as quit.
go        (\g) Send command to mysql server.
help      (\h) Display this help.
nopager   (\n) Disable pager, print to stdout.
notee     (\t) Don't write into outfile.
pager     (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print     (\p) Print current command.
prompt    (\R) Change your mysql prompt.
quit      (\q) Quit mysql.
rehash    (\#) Rebuild completion hash.
source    (\.) Execute an SQL script file. Takes a file name as an argument.
status    (\s) Get status information from the server.
system    (\!) Execute a system shell command.
tee       (\T) Set outfile [to_outfile]. Append everything into given outfile.
use       (\u) Use another database. Takes database name as argument.
charset   (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings  (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.

どうやって出来てるのか

  • Python Prompt Toolkit
  • Sqlparse

を使って出来てるらしい

postgresql もあるの?

ある。 pgcli というのがある。

参考資料

http://mycli.net/install

https://orebibou.com/2017/05/sql%E8%A8%98%E8%BF%B0%E6%99%82%E3%81%AB%E3%82%AA%E3%83%BC%E3%83%88%E3%82%B3%E3%83%B3%E3%83%97%E3%83%AA%E3%83%BC%E3%83%88%E3%81%97%E3%81%A6%E3%81%8F%E3%82%8C%E3%82%8B%E3%82%B3%E3%83%9E%E3%83%B3/