1 <h1 align="center">SQLite Source Repository</h1>
3 This repository contains the complete source code for the SQLite database
4 engine. Some test scripts are also included. However, many other test scripts
5 and most of the documentation are managed separately.
7 If you are reading this on a Git mirror someplace, you are doing it wrong.
8 The [official repository](https://www.sqlite.org/src/) is better. Go there
13 SQLite sources are managed using the
14 [Fossil](https://www.fossil-scm.org/), a distributed version control system
15 that was specifically designed to support SQLite development.
16 If you do not want to use Fossil, you can download tarballs or ZIP
17 archives or [SQLite archives](https://sqlite.org/cli.html#sqlar) as follows:
19 * Lastest trunk check-in as
20 [Tarball](https://www.sqlite.org/src/tarball/sqlite.tar.gz),
21 [ZIP-archive](https://www.sqlite.org/src/zip/sqlite.zip), or
22 [SQLite-archive](https://www.sqlite.org/src/sqlar/sqlite.sqlar).
25 [Tarball](https://www.sqlite.org/src/tarball/sqlite.tar.gz?r=release),
26 [ZIP-archive](https://www.sqlite.org/src/zip/sqlite.zip?r=release), or
27 [SQLite-archive](https://www.sqlite.org/src/sqlar/sqlite.sqlar?r=release).
29 * For other check-ins, substitute an appropriate branch name or
30 tag or hash prefix for "release" in the URLs of the previous
31 bullet. Or browse the [timeline](https://www.sqlite.org/src/timeline)
32 to locate the check-in desired, click on its information page link,
33 then click on the "Tarball" or "ZIP Archive" links on the information
36 If you do want to use Fossil to check out the source tree,
37 first install Fossil version 2.0 or later.
38 (Source tarballs and precompiled binaries available
39 [here](https://www.fossil-scm.org/fossil/uv/download.html). Fossil is
40 a stand-alone program. To install, simply download or build the single
41 executable file and put that file someplace on your $PATH.)
42 Then run commands like this:
46 fossil clone https://www.sqlite.org/src sqlite.fossil
47 fossil open sqlite.fossil
49 After setting up a repository using the steps above, you can always
50 update to the lastest version using:
52 fossil update trunk ;# latest trunk check-in
53 fossil update release ;# latest official release
55 Or type "fossil ui" to get a web-based user interface.
59 First create a directory in which to place
60 the build products. It is recommended, but not required, that the
61 build directory be separate from the source directory. Cd into the
62 build directory and then from the build directory run the configure
63 script found at the root of the source tree. Then run "make".
67 tar xzf sqlite.tar.gz ;# Unpack the source tree into "sqlite"
68 mkdir bld ;# Build will occur in a sibling directory
69 cd bld ;# Change to the build directory
70 ../sqlite/configure ;# Run the configure script
71 make ;# Run the makefile.
72 make sqlite3.c ;# Build the "amalgamation" source file
73 make test ;# Run some tests (requires Tcl)
75 See the makefile for additional targets.
77 The configure script uses autoconf 2.61 and libtool. If the configure
78 script does not work out for you, there is a generic makefile named
79 "Makefile.linux-gcc" in the top directory of the source tree that you
80 can copy and edit to suit your needs. Comments on the generic makefile
81 show what changes are needed.
85 On Windows, all applicable build products can be compiled with MSVC.
86 First open the command prompt window associated with the desired compiler
87 version (e.g. "Developer Command Prompt for VS2013"). Next, use NMAKE
88 with the provided "Makefile.msc" to build one of the supported targets.
94 nmake /f Makefile.msc TOP=..\sqlite
95 nmake /f Makefile.msc sqlite3.c TOP=..\sqlite
96 nmake /f Makefile.msc sqlite3.dll TOP=..\sqlite
97 nmake /f Makefile.msc sqlite3.exe TOP=..\sqlite
98 nmake /f Makefile.msc test TOP=..\sqlite
100 There are several build options that can be set via the NMAKE command
101 line. For example, to build for WinRT, simply add "FOR_WINRT=1" argument
102 to the "sqlite3.dll" command line above. When debugging into the SQLite
103 code, adding the "DEBUG=1" argument to one of the above command lines is
106 SQLite does not require [Tcl](http://www.tcl.tk/) to run, but a Tcl installation
107 is required by the makefiles (including those for MSVC). SQLite contains
108 a lot of generated code and Tcl is used to do much of that code generation.
112 Most of the core source files are in the **src/** subdirectory. The
113 **src/** folder also contains files used to build the "testfixture" test
114 harness. The names of the source files used by "testfixture" all begin
116 The **src/** also contains the "shell.c" file
117 which is the main program for the "sqlite3.exe"
118 [command-line shell](https://sqlite.org/cli.html) and
119 the "tclsqlite.c" file which implements the
120 [Tcl bindings](https://sqlite.org/tclsqlite.html) for SQLite.
121 (Historical note: SQLite began as a Tcl
122 extension and only later escaped to the wild as an independent library.)
124 Test scripts and programs are found in the **test/** subdirectory.
125 Addtional test code is found in other source repositories.
126 See [How SQLite Is Tested](http://www.sqlite.org/testing.html) for
127 additional information.
129 The **ext/** subdirectory contains code for extensions. The
130 Full-text search engine is in **ext/fts3**. The R-Tree engine is in
131 **ext/rtree**. The **ext/misc** subdirectory contains a number of
132 smaller, single-file extensions, such as a REGEXP operator.
134 The **tool/** subdirectory contains various scripts and programs used
135 for building generated source code files or for testing or for generating
136 accessory programs such as "sqlite3_analyzer(.exe)".
138 ### Generated Source Code Files
140 Several of the C-language source files used by SQLite are generated from
141 other sources rather than being typed in manually by a programmer. This
142 section will summarize those automatically-generated files. To create all
143 of the automatically-generated files, simply run "make target_source".
144 The "target_source" make target will create a subdirectory "tsrc/" and
145 fill it with all the source files needed to build SQLite, both
146 manually-edited files and automatically-generated files.
148 The SQLite interface is defined by the **sqlite3.h** header file, which is
149 generated from src/sqlite.h.in, ./manifest.uuid, and ./VERSION. The
150 [Tcl script](http://www.tcl.tk) at tool/mksqlite3h.tcl does the conversion.
151 The manifest.uuid file contains the SHA3 hash of the particular check-in
152 and is used to generate the SQLITE\_SOURCE\_ID macro. The VERSION file
153 contains the current SQLite version number. The sqlite3.h header is really
154 just a copy of src/sqlite.h.in with the source-id and version number inserted
155 at just the right spots. Note that comment text in the sqlite3.h file is
156 used to generate much of the SQLite API documentation. The Tcl scripts
157 used to generate that documentation are in a separate source repository.
159 The SQL language parser is **parse.c** which is generate from a grammar in
160 the src/parse.y file. The conversion of "parse.y" into "parse.c" is done
161 by the [lemon](./doc/lemon.html) LALR(1) parser generator. The source code
162 for lemon is at tool/lemon.c. Lemon uses the tool/lempar.c file as a
163 template for generating its parser.
165 Lemon also generates the **parse.h** header file, at the same time it
166 generates parse.c. But the parse.h header file is
167 modified further (to add additional symbols) using the ./addopcodes.tcl
170 The **opcodes.h** header file contains macros that define the numbers
171 corresponding to opcodes in the "VDBE" virtual machine. The opcodes.h
172 file is generated by the scanning the src/vdbe.c source file. The
173 Tcl script at ./mkopcodeh.tcl does this scan and generates opcodes.h.
174 A second Tcl script, ./mkopcodec.tcl, then scans opcodes.h to generate
175 the **opcodes.c** source file, which contains a reverse mapping from
176 opcode-number to opcode-name that is used for EXPLAIN output.
178 The **keywordhash.h** header file contains the definition of a hash table
179 that maps SQL language keywords (ex: "CREATE", "SELECT", "INDEX", etc.) into
180 the numeric codes used by the parse.c parser. The keywordhash.h file is
181 generated by a C-language program at tool mkkeywordhash.c.
183 The **pragma.h** header file contains various definitions used to parse
184 and implement the PRAGMA statements. The header is generated by a
185 script **tool/mkpragmatab.tcl**. If you want to add a new PRAGMA, edit
186 the **tool/mkpragmatab.tcl** file to insert the information needed by the
187 parser for your new PRAGMA, then run the script to regenerate the
188 **pragma.h** header file.
192 All of the individual C source code and header files (both manually-edited
193 and automatically-generated) can be combined into a single big source file
194 **sqlite3.c** called "the amalgamation". The amalgamation is the recommended
195 way of using SQLite in a larger application. Combining all individual
196 source code files into a single big source code file allows the C compiler
197 to perform more cross-procedure analysis and generate better code. SQLite
198 runs about 5% faster when compiled from the amalgamation versus when compiled
199 from individual source files.
201 The amalgamation is generated from the tool/mksqlite3c.tcl Tcl script.
202 First, all of the individual source files must be gathered into the tsrc/
203 subdirectory (using the equivalent of "make target_source") then the
204 tool/mksqlite3c.tcl script is run to copy them all together in just the
205 right order while resolving internal "#include" references.
207 The amalgamation source file is more than 200K lines long. Some symbolic
208 debuggers (most notably MSVC) are unable to deal with files longer than 64K
209 lines. To work around this, a separate Tcl script, tool/split-sqlite3c.tcl,
210 can be run on the amalgamation to break it up into a single small C file
211 called **sqlite3-all.c** that does #include on about seven other files
212 named **sqlite3-1.c**, **sqlite3-2.c**, ..., **sqlite3-7.c**. In this way,
213 all of the source code is contained within a single translation unit so
214 that the compiler can do extra cross-procedure optimization, but no
215 individual source file exceeds 32K lines in length.
217 ## How It All Fits Together
219 SQLite is modular in design.
220 See the [architectural description](http://www.sqlite.org/arch.html)
221 for details. Other documents that are useful in
222 (helping to understand how SQLite works include the
223 [file format](http://www.sqlite.org/fileformat2.html) description,
224 the [virtual machine](http://www.sqlite.org/opcode.html) that runs
225 prepared statements, the description of
226 [how transactions work](http://www.sqlite.org/atomiccommit.html), and
227 the [overview of the query planner](http://www.sqlite.org/optoverview.html).
229 Years of effort have gone into optimizating SQLite, both
230 for small size and high performance. And optimizations tend to result in
231 complex code. So there is a lot of complexity in the current SQLite
232 implementation. It will not be the easiest library in the world to hack.
236 * **sqlite.h.in** - This file defines the public interface to the SQLite
237 library. Readers will need to be familiar with this interface before
238 trying to understand how the library works internally.
240 * **sqliteInt.h** - this header file defines many of the data objects
241 used internally by SQLite. In addition to "sqliteInt.h", some
242 subsystems have their own header files.
244 * **parse.y** - This file describes the LALR(1) grammar that SQLite uses
245 to parse SQL statements, and the actions that are taken at each step
246 in the parsing process.
248 * **vdbe.c** - This file implements the virtual machine that runs
249 prepared statements. There are various helper files whose names
250 begin with "vdbe". The VDBE has access to the vdbeInt.h header file
251 which defines internal data objects. The rest of SQLite interacts
252 with the VDBE through an interface defined by vdbe.h.
254 * **where.c** - This file (together with its helper files named
255 by "where*.c") analyzes the WHERE clause and generates
256 virtual machine code to run queries efficiently. This file is
257 sometimes called the "query optimizer". It has its own private
258 header file, whereInt.h, that defines data objects used internally.
260 * **btree.c** - This file contains the implementation of the B-Tree
261 storage engine used by SQLite. The interface to the rest of the system
262 is defined by "btree.h". The "btreeInt.h" header defines objects
263 used internally by btree.c and not published to the rest of the system.
265 * **pager.c** - This file contains the "pager" implementation, the
266 module that implements transactions. The "pager.h" header file
267 defines the interface between pager.c and the rest of the system.
269 * **os_unix.c** and **os_win.c** - These two files implement the interface
270 between SQLite and the underlying operating system using the run-time
271 pluggable VFS interface.
273 * **shell.c.in** - This file is not part of the core SQLite library. This
274 is the file that, when linked against sqlite3.a, generates the
275 "sqlite3.exe" command-line shell. The "shell.c.in" file is transformed
276 into "shell.c" as part of the build process.
278 * **tclsqlite.c** - This file implements the Tcl bindings for SQLite. It
279 is not part of the core SQLite library. But as most of the tests in this
280 repository are written in Tcl, the Tcl language bindings are important.
282 * **test*.c** - Files in the src/ folder that begin with "test" go into
283 building the "testfixture.exe" program. The testfixture.exe program is
284 an enhanced Tcl shell. The testfixture.exe program runs scripts in the
285 test/ folder to validate the core SQLite code. The testfixture program
286 (and some other test programs too) is build and run when you type
289 * **ext/misc/json1.c** - This file implements the various JSON functions
290 that are build into SQLite.
292 There are many other source files. Each has a succinct header comment that
293 describes its purpose and role within the larger system.
298 The main SQLite webpage is [http://www.sqlite.org/](http://www.sqlite.org/)
299 with geographically distributed backups at
300 [http://www2.sqlite.org/](http://www2.sqlite.org) and
301 [http://www3.sqlite.org/](http://www3.sqlite.org).