爲什麼要編譯自己的sqlite
默認sqlite包是爲了輕量化、快速、低消耗而配置的,很多功能都閹割掉了。
然而,我們有時候使用sqlite並不是爲了節省資源,而是sqlite用起來太方便了,單庫單文件,清晰而美好。
這時候如果我們需要索取更多的功能,比如json的解析功能(json函數說明見此處),或者希望利用多線程做排序,提高速度,那麼就需要手動編譯一份sqlite。
可用的擴展功能
- Json1 - JSON Integration → SQL functions for creating, parsing, and querying JSON content.
- FTS5 - Full Text Search → A description of the SQLite Full Text Search (FTS5) extension.
- FTS3 - Full Text Search → A description of the SQLite Full Text Search (FTS3) extension.
- R-Tree Module → A description of the SQLite R-Tree extension. An R-Tree is a specialized data structure that supports fast multi-dimensional range queries often used in geospatial systems.
- Sessions → The Sessions extension allows change to an SQLite database to be captured in a compact file which can be reverted on the original database (to implement "undo") or transferred and applied to another similar database.
- Run-Time Loadable Extensions → A general overview on how run-time loadable extensions work, how they are compiled, and how developers can create their own run-time loadable extensions for SQLite.
- SQLite Android Bindings → Information on how to deploy your own private copy of SQLite on Android, bypassing the built-in SQLite, but using the same Java interface.
- Dbstat Virtual Table → The DBSTAT virtual table reports on the sizes and geometries of tables storing content in an SQLite database, and is the basis for the [sqlite3_analyzer] utility program.
- Csv Virtual Table → The CSV virtual table allows SQLite to directly read and query [https://www.ietf.org/rfc/rfc4180.txt|RFC 4180] formatted files.
- Carray → CARRAY is a [table-valued function] that allows C-language arrays to be used in SQL queries.
- generate_series → A description of the generate_series() [table-valued function].
- Spellfix1 → The spellfix1 extension is an experiment in doing spelling correction for [full-text search].
列表見【官網】
這些擴展功能在哪裏下載?
不需要額外下載,只需要去官網下載sqlite源代碼壓縮包:https://www.sqlite.org/download.html
比如我下載的是這一份:
sqlite-amalgamation-3310100.zip (2.28 MiB) |
C source code as an amalgamation, version 3.31.1. (sha1: a58e91a39b7b4ab720dbc843c201fb6a18eaf32b) |
裏面只包含幾個c語言源代碼和頭文件,沒有makefile之類的東西。
你會發現這些源代碼的體積非常大,這是因爲上述所有擴展功能,都早已被塞進了統一的源代碼文件裏。
只是平時編譯的時候默認不帶擴展而已。源代碼裏其實都有。
那麼如何啓動這些擴展功能呢?
編譯並指定開啓擴展功能
在linux或mac下,進入源代碼所在文件夾,輸入以下編譯指令:
gcc -O2 -I. \
-DSQLITE_DEFAULT_WORKER_THREADS=4 \
-DSQLITE_MAX_WORKER_THREADS=6 \
-DSQLITE_ENABLE_FTS4 \
-DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_JSON1 \
-DSQLITE_ENABLE_RTREE -DSQLITE_ENABLE_EXPLAIN_COMMENTS \
-DHAVE_USLEEP -DHAVE_READLINE \
shell.c sqlite3.c -ldl -lreadline -lncurses -lpthread -o sqlite3
注意,這條指令不是野論壇魔改指令,而是官網編譯指南所提供的方法:https://www.sqlite.org/howtocompile.html
其中,O2 的速度優化參照【說明】【文檔】,pthread優化參照【文檔】,編譯選項參照【文檔】,編譯出來1.7M左右的大小。
編譯成功後,將sqlite3二進制文件複製到/usr/local/bin/ 下面就可以了。
針對Windows的編譯區別
在win下,進入源碼文件夾,使用MSVC的 cl 編譯器(c-languange):
cl shell.c sqlite3.c -Fesqlite3.exe
講技術,說人話。
Aurora 極光城