2020年9月28日月曜日

SQLite 3 コマンドに文字列を出力する「.print」について

概要

SQLite3 のコマンドラインツールで文字列を出力するだけのコマンド。
コマンドプロンプトやBashで使う echoコマンドに近い。
.outputコマンドと組み合わせて、コメントを出力する・ デバッグメッセージを出力する等の用途で利用することが出来る。

ヘルプの内容


sqlite> .help .print
.print STRING...         Print literal STRING
                            

実行例

環境
  • Windows 10 64bit
  • QLite3 Command-Line Shell ver.3.33.0
テキストをコンソールに出力する

sqlite> .print Hello world
Hello world
                                
特殊文字も利用できる

改行


sqlite> .print Hello\n world
Hello
world
                                

タブ


sqlite> .print \tHello world
Hello world
                                
文字列を明示する

「'」で囲むとリテラルとして明示出来る
「"」で囲むと特殊文字を利用出来る


sqlite> .print    Hello world
Hello world
                                

sqlite> .print '    Hello world'
    Hello world
                                

sqlite> .print '\tHello world'
\tHello world
                                

sqlite> .print "\tHello world"
    Hello world
                                

.outputコマンドでSQLの結果とコメントをテキストファイルに出力する


sqlite> .output sample.txt
sqlite> .print '# This is sample data'
sqlite> select * from sample_table;
sqlite> .output
                            

sample.txt


# This is sample data
001|sample01
002|sample02
                            

参考URL