1
0

More than 5 years have passed since last update.

macでSQLiteをビルドしてみる

Posted at

手元のMac(El Capitan、バージョン10.11.6)でSQLiteをビルドしてみたので、メモります。

SQLiteのソースコードはこちらからダウンロードしました。記事執筆時点(2017-12-09)での最新は3.21.0です。
https://www.sqlite.org/download.html

SQLite_src_download.png

sqlite-amalgamation-3210000.zipをダウンロードして、適当なディレクトリにコピーし、解凍します。

$ mkdir ~/work/sqlite
$ cd ~/work/sqlite
$ cp ~/Downloads/sqlite-amalgamation-3210000.zip .
$ unzip sqlite-amalgamation-3210000.zip
Archive:  sqlite-amalgamation-3210000.zip
   creating: sqlite-amalgamation-3210000/
  inflating: sqlite-amalgamation-3210000/sqlite3ext.h  
  inflating: sqlite-amalgamation-3210000/sqlite3.c  
  inflating: sqlite-amalgamation-3210000/sqlite3.h  
  inflating: sqlite-amalgamation-3210000/shell.c  

sqlite3.cshell.cをコンパイルします。

$ cd sqlite-amalgamation-3210000
$ ls
shell.c sqlite3.c sqlite3.h sqlite3ext.h
$ gcc -o sqlite3 sqlite3.c shell.c

起動してみます。

$ ./sqlite3
SQLite version 3.21.0 2017-10-24 18:55:49
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> SELECT sqlite_version();
3.21.0
sqlite> 

バージョン3.21.0であることが確認できました。

コマンドラインでの編集支援機能を追加

こうしてビルドしたsqlite3のCLIは、キーボードの左右キーでカーソル移動できません。

sqlite> ^[[D^[[C

左右キーを押すと上記のように文字が入力されてしまいます。

SQLiteのドキュメント How To Compile SQLiteによると、コンパイル時に以下のようにオプションを追加すると、コマンドラインの編集機能が追加されるとのこと。

gcc -o sqlite3 shell.c sqlite3.c -DHAVE_READLINE -lreadline -lncurses

実際やってみると、こうしてビルドしたCLIでは、左右キーでカーソルが移動できるのはもちろん、過去に実行したSQL文も上下キーでたどることができました。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0