SQLite源码剖析(1)

声明:本SQLite源码剖析系列为刘兴(http://deepfuture.iteye.com/)原创,未经笔者授权,任何人和机构不能转载

什么是 sqlite ?

sqlite 是一款轻量级的、基于文件的嵌入式数据库,2000年就已经诞生,经过7年多的发展,直到今天已经成为最流行的嵌入式数据库,包括google在内的公司在其桌面软件中亦使用 sqlite 存储用户数据。由此可以看出,已经没有任何理由去怀疑sqlite的稳定性了。


sqlite的优势?

1. 免配置,和access一样,只要把数据库文件通过ftp上传到服务器上就可以使用,不需要服务器的额外支持

2. 备份方便,因为只是一个文件,只要复制一份该文件,就能备份整个数据库

3. 虽然是轻量级数据库,但他支持最大 2tb 的单个库文件。

4. 快,无与伦比的快。经过实际测试,在几百万记录的情况下,sqlite的插入和查询速度和 mysql 不分上下,快于 sql server,10倍于 access (但这并不意味着它可以替代 sql server )

 

SQLite源码下载 

    在SQLITE主页上下载源码,选择右边出现以下字样的进行下载



 

 

 

源码分析

我们先分析主程序sqlite3.c

/******************************************************************************

** This file is an amalgamation of many separate C source files from SQLite

** version 3.6.23.1.  By combining all the individual C code files into this

** single large file, the entire code can be compiled as a one translation

** unit.  This allows many compilers to do optimizations that would not be

** possible if the files were compiled separately.  Performance improvements

** of 5% are more are commonly seen when SQLite is compiled as a single

** translation unit.

**

** This file is all you need to compile SQLite.  To use SQLite in other

** programs, you need this file and the "sqlite3.h" header file that defines

** the programming interface to the SQLite library.  (If you do not have

** the "sqlite3.h" header file at hand, you will find a copy embedded within

** the text of this file.  Search for "Begin file sqlite3.h" to find the start

** of the embedded sqlite3.h header file.) Additional code files may be needed

** if you want a wrapper to interface SQLite with your choice of programming

** language. The code for the "sqlite3" command-line shell is also in a

** separate file. This file contains only code for the core SQLite library.

*/

//主程序定义SQLITE核心:define SQLITE_CORE 1

//主程序定义SQLITE_API。

//主程序序版本为合并后的源代码:define SQLITE_AMALGAMATION 1

#define SQLITE_CORE 1

#define SQLITE_AMALGAMATION 1

#ifndef SQLITE_PRIVATE

# define SQLITE_PRIVATE static

#endif

#ifndef SQLITE_API

# define SQLITE_API

#endif

/************** Begin file sqliteInt.h ***************************************/

/*

** 2001 September 15

**

** The author disclaims copyright to this source code.  In place of

** a legal notice, here is a blessing:

**

**    May you do good and not evil.

**    May you find forgiveness for yourself and forgive others.

**    May you share freely, never taking more than you give.

**

*************************************************************************

** Internal interface definitions for SQLite.

**

*/

//SQLITE内部接口定义

#ifndef _SQLITEINT_H_

#define _SQLITEINT_H_

//如果基础操作系统支持,可用#define开启大于2G的单个文件支持。

/*

** These #defines should enable >2GB file support on POSIX if the

** underlying operating system supports it.  If the OS lacks

** large file support, or if the OS is windows, these should be no-ops.

**_LARGEFILE_SOURCE宏必须在任何#include前使用

** Ticket #2739:  The _LARGEFILE_SOURCE macro must appear before any

** system #includes.  Hence, this block of code must be the very first

** code in all source files.

**在编译命令行使用-DSQLITE_DISABLE_LFS可禁止大文件支持,

** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch

** on the compiler command line.  This is necessary if you are compiling

** on a recent machine (ex: Red Hat 7.2) but you want your code to work

** on an older machine (ex: Red Hat 6.0).  If you compile on Red Hat 7.2

** without this option, LFS is enable.  But LFS does not exist in the kernel

** in Red Hat 6.0, so the code won't work.  Hence, for maximum binary

** portability you should omit LFS.

**

** Similar is true for Mac OS X.  LFS is only supported on Mac OS X 9 and later.

*/

//如果没禁止大文件支持,则定义相关常量

#ifndef SQLITE_DISABLE_LFS

# define _LARGE_FILE       1

# ifndef _FILE_OFFSET_BITS

#   define _FILE_OFFSET_BITS 64

# endif

# define _LARGEFILE_SOURCE 1

#endif

 

/*

** Include the configuration header output by 'configure' if we're using the

** autoconf-based build

*/

//如果使用autoconf-based构建,则include "config.h"

#ifdef _HAVE_SQLITE_CONFIG_H

#include "config.h"

#endif

 

/************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/

/************** Begin file sqliteLimit.h *************************************/

/*

** 2007 May 7

**

** The author disclaims copyright to this source code.  In place of

** a legal notice, here is a blessing:

**

**    May you do good and not evil.

**    May you find forgiveness for yourself and forgive others.

**    May you share freely, never taking more than you give.

**

*************************************************************************

**

** This file defines various limits of what SQLite can process.

*/

 

/*

** The maximum length of a TEXT or BLOB in bytes.   This also

** limits the size of a row in a table or index.

**

** The hard limit is the ability of a 32-bit signed integer

** to count the size: 2^31-1 or 2147483647.

*/

//最大32位有符号整数

#ifndef SQLITE_MAX_LENGTH

# define SQLITE_MAX_LENGTH 1000000000

#endif

 

/*

** This is the maximum number of

**以下这些项的最大值:

**表中的列、索引中的列、视图的列、update的set从句的数量、select

**返回的字段数、GROUP BY 和ORDER BY的字段数、INSERT的values从句

**    * Columns in a table

**    * Columns in an index

**    * Columns in a view

**    * Terms in the SET clause of an UPDATE statement

**    * Terms in the result set of a SELECT statement

**    * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.

**    * Terms in the VALUES clause of an INSERT statement

**

** The hard upper limit here is 32676.  Most database people will

** tell you that in a well-normalized database, you usually should

** not have more than a dozen or so columns in any table.  And if

** that is the case, there is no point in having more than a few

** dozen values in any of the other situations described above.

*/

/*

**以下这些项的最大值:

**表中的列、索引中的列、视图的列、update的set从句的数量、select

**返回的字段数、GROUP BY 和ORDER BY的字段数、INSERT的values从句

*/

#ifndef SQLITE_MAX_COLUMN

# define SQLITE_MAX_COLUMN 2000

#endif

 

/*

** The maximum length of a single SQL statement in bytes.

**

** It used to be the case that setting this value to zero would

** turn the limit off.  That is no longer true.  It is not possible

** to turn this limit off.

*/

//SQL语句的最大长度

#ifndef SQLITE_MAX_SQL_LENGTH

# define SQLITE_MAX_SQL_LENGTH 1000000000

#endif

 

/*

** The maximum depth of an expression tree. This is limited to

** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might

** want to place more severe limits on the complexity of an

** expression.

**

** A value of 0 used to mean that the limit was not enforced.

** But that is no longer true.  The limit is now strictly enforced

** at all times.

*/

//解释树的最大深度

#ifndef SQLITE_MAX_EXPR_DEPTH

# define SQLITE_MAX_EXPR_DEPTH 1000

#endif

 

/*

** The maximum number of terms in a compound SELECT statement.

** The code generator for compound SELECT statements does one

** level of recursion for each term.  A stack overflow can result

** if the number of terms is too large.  In practice, most SQL

** never has more than 3 or 4 terms.  Use a value of 0 to disable

** any limit on the number of terms in a compount SELECT.

*/

//复合SQL语句的最大项数

#ifndef SQLITE_MAX_COMPOUND_SELECT

# define SQLITE_MAX_COMPOUND_SELECT 500

#endif

SQLite 源码分析 SQLite 源码核心分析 SQLite作为轻量级嵌入式数据库,采用分层架构设计: 解析层:Tokenizer与Parser将SQL转为语法树 执行层:Code Generator生成字节码,VDBE虚拟机执行指令 存储层:B+树管理数据,Pager处理I/O与事务 关键数据结构包括BTree对象、页面存储单元及遍历游标。SQL执行流程分为解析→字节码生成→虚拟机执行→结果返回四阶段,通过WAL日志、页面缓存等优化性能。建议从sqlite3_prepare_v2()入口切入,重点研究btree.c、vdbe 阅读详情

相关推荐

从.svn到源码沦陷:一次完整的SVN信息泄露漏洞实战剖析

本文深入剖析了SVN信息泄露漏洞的实战过程,从.svn目录暴露到源码泄露的全链条攻击手法。通过解析wc.db数据库和pristine目录,攻击者可快速还原完整代码库,文中还提供了自动化脚本和防御配置建议,帮助开发者有效防范此类安全风险。

weixin_33713707的博客 840

SQLite源码剖析

/****************************************************************************** ** This file is an amalgamation of many separate C source files from SQLite ** version 3.6.23.1. By combining all the

xyjikl 2178

DeepSeek Harness 源码级深度剖析:全插件架构 Agent 框架从入门到实战

基于 dsh v0.1.0-rc.8 源码,深度解析全插件架构 Agent 框架的 5 大核心包(Session/Agent/Agent Loop/Tools/defineTool)关键设计,剖析 Cordis 插件框架的时空可组合性与 Capability Seam 三角色分离模式,并附带一个可直接投产的 Test Guardian 测试守护插件(1658 行代码、46 个测试用例),最后提炼 10 条架构设计铁律。全文约 25000 字。

johnsong2009的博客 2333

Sqlite源码解读(一)

Sqlite源码解读一 本博客系列将在接下来的三个月内开始对Sqlite的部分源码进行解读。Sqlite源码解读部分将由

qq_53825872的博客 3854

Sqlite源码解读(六)

2021SC@SDUSC 前面几篇详细讲解了VFS的内存分配和共享缓存机制。 本篇将详细介绍各种字符串相互转换的方法及基于这些方法的公共包装器。 先看一个将UTF-8字符串与Microsoft Unicode相互转换的方法,由sqlite3_malloc()提供保存返回字符串的空间。 /*将utf-8字符串转换为microsoft Unicode。*/ static LPWSTR winUtf8ToUnicode(const char *zText){ int nChar; L..

qq_53825872的博客 1320

Sqlite源码解读(二)

本篇开始解读代码: 开头一堆注释主要说明此文件是基于Windows NT操作系统内核(区分于Unix) 开始分析代码: #if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL) # error "WAL mode requires support from the Windows NT kernel, compile\ with SQLITE_OMIT_WAL." 如果不是windows NT 操作系统且没有引入WAL模式,则会报错。..

qq_53825872的博客 1742

Sqlite3.78移植到VxWorks6.6

移植sqlite3.78到VxWorks6.6是一个痛苦的过程,网上的资源也是少得可怜,功夫不负有心人,昨天自己通过windows与wxworks单步跟踪比较运行状况,折腾了几天的问题总算初步解决了,还有一些像线程互斥遗留问题日后解决吧。迷茫之时还特意向sqlite原作者请教,尽

它山之石,可以攻玉 7591

读了SQLite源码,发现一个人用C写出了比大多数团队用C++写的数据库更可靠的系统——SQLite的6个工程哲学

一组数据。SQLite的核心库大约15.5万行C代码。测试代码和测试脚本加起来超过9200万行——是核心代码的590倍。100%的分支覆盖率,100%的MC/DC覆盖率(Modified Condition/Decision Coverage,这是航空软件DO-178B标准的最高要求)。全球部署量超过1万亿份,运行在每一部智能手机、每一个浏览器、每一台电视机里。而这一切的主要作者,是一个人:D. Richard Hipp。我最近花了两周时间读SQLite的源码。不是走马观花看个架构图,是真的把这个25万行的

weixin_45715405的博客 132

SQLite源码编译教程

SQLite源码编译安装SQLite简介编译安装源码下载编译过程demo实例总结 SQLite简介 你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识。 SQLite实现了小型,快速,自包含,高可靠性,功能齐全的SQL数据库引擎,以ASNI-C语言编写。 SQLite是世界上最常用的数据库引擎。 SQLite内置于所有手机和大多数计算机中,并捆绑在人们每天使用的无数其他应用程序中。 同时

weixin_46912937的博客 2075

深入剖析SQLite3源码与核心机制实战

SQLite 允许用户通过注册自定义 SQL 函数:int nArg,若xFunc非空,则为标量函数;若xStep和xFinal非空,则为聚合函数(类似 COUNT/SUM)。示例:注册一个字符串长度函数 LENGTH_PLUS_ONEint argc,) {} else {// 注册之后即可在 SQL 中使用:此类机制广泛用于地理计算、JSON处理、加密函数等场景。graph TDB --> C{成功?E --> F{是否有行?

weixin_28729843的博客 697

SQLite3源码剖析与实战

SQLite3是一个轻量级的嵌入式关系型数据库,广泛应用于移动设备、桌面应用程序和小型项目中。它不需要独立的服务器进程运行,通过直接将数据库文件集成到应用程序中,简化了数据库的部署和管理。SQLite3非常注重跨平台性和对标准SQL的支持,使其在需要轻便、快速和灵活数据库解决方案的场景下成为首选。SQLite3作为一款轻量级的数据库管理系统,其核心存储机制依赖于数据页(page)和B-Tree索引结构。数据页是SQLite进行数据读写的最小单位,具有固定大小,默认为1024字节。

weixin_42356162的博客 454

SQLITE源码剖析(8)

声明:本SQLite源码剖析系列为刘兴(http://deepfuture.iteye.com/)原创,未经笔者授权,任何人和机构不能转载 /*库线程安全 ** CAPI3REF: Test To See If The Library Is Threadsafe **SQLITE_THREADSAFE预处理宏编译时选项设为0,则忽略SQLITE的互斥代码, **此时,sqlite3_thr...

深未来技术 164

SQLite 源文件

SQLite 源文件

SQLITE源码剖析

http://www.iteye.com/blogs/subjects/deepfuture

xcw_1987的博客 529

sqlite 源代码

sqlite 源代码 sqlite-amalgamation-3.6.1.tar.gz

sqlite源码及分析

文档包含sqlite源码,源码分析,以及使用教程

sqlite-1.0.0源码执行的基本流程概述

sqlite-1.0.0原理概述 sqlite是一款嵌入式的轻量级的数据库,首个版本诞生于2000年,该数据库遵守ACID的关系数据库管理系统,SQLite不是一个cs架构的数据库引擎,而是被集成在用户程序中,SQLite实现了大部分的SQL标准。本文主要概述sqlite-1.0.0版本的启动的简单流程。 sqlite-1.0.0的源码 sqlite源码目录 主要包括了源码目录src,vdbe....

qq_33339479的博客 1829

SolidWorks+ADAMS双验证:你的机械臂力矩计算真的靠谱吗?

本文探讨了机械臂力矩计算中手工计算的局限性,并介绍了SolidWorks和ADAMS双验证方法的重要性。通过实际案例展示了动态工况下传统计算方法的误差,详细解析了SolidWorks精准质心捕捉和ADAMS动力学仿真的关键技术,帮助工程师构建计算-仿真-实测闭环验证流程,确保机械臂设计的可靠性和安全性。

grafana6viz的博客 230

ubuntu sqlite3源码编译

1、首先从SQLite Download Page网站下载最新sqlite3源码, 2、选择sqlite3.c和sqlite3.h,sqlite3ext.h加载到工程中。 3、编写测试main.c函数: #include <stdio.h> #include <stdlib.h> #include "sqlite3.h" int main() { sqlite3 *db; int nResult = sqlite3_open("test.db", &

u010179747的博客 1549

逆向拆解Android车机:VehicleHal如何通过HIDL与CarService通信?

本文深入剖析了Android Automotive OS中VehicleHal与CarService通过HIDL进行通信的底层机制。从逆向工程视角出发,详细介绍了如何捕获和分析HIDL调用日志,解析关键属性数据结构,并演示了利用SocketComm工具进行恶意属性注入攻击的实战路径,为车机系统安全评估提供了技术参考。

soda5的博客 722

科研笔记:怎样写好博士论文?

博士论文外审避坑指南与写作要点 博士论文外审的核心关注点呈现5级优先级:科研成果(硬指标)>创新性>逻辑结构与场景合理性>文献综述与规范性>工作量验证。外审专家会重点核查:1)成果清单是否达标;2)创新点是否真实明确;3)章节逻辑是否自洽、应用场景是否细化;4)文献综述是否构建"研究坐标系";5)实验/理论支撑是否充分。需特别注意三大雷区:科学错误(直接挂稿)、伪创新、场景不合理。写作时应强化选题依据的场景细化,通过"发展阶段+跨领域对比"提升文献综述质量。

Grow into a more wonderful you 1065
上一篇: 12款个人防火墙软件横向评测
下一篇: sqlite源码剖析(2)
deepfuture
博客等级 码龄17年 211粉丝 1287原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值