非常教程

Sqlite参考手册

其他 | Miscellaneous

Run-Time Loadable Extensions

1.概述

2.加载扩展

3.编译可加载的扩展

4.编程可加载扩展

4.1.示例扩展

5.持久可装载扩展

6.静态链接运行时可加载扩展

7.实施细节

SQLite能够在运行时加载扩展(包括新的应用程序定义的SQL函数,整理序列,虚拟表和VFS)。此功能允许扩展的代码与应用程序分开开发和测试,然后根据需要加载。

扩展也可以静态链接到应用程序。下面显示的代码模板与静态链接的扩展一样好,因为它可以作为运行时可装载的扩展,除了您应该为入口点函数(“sqlite3_extension_init”)指定不同的名称以避免名称冲突(如果您的应用程序包含两个或更多的扩展。

SQLite扩展是一个共享库或DLL。要加载它,您需要为SQLite提供包含共享库或DLL的文件的名称以及初始化扩展的入口点。在C代码中,使用sqlite3_load_extension()API提供此信息。有关其他信息,请参阅该例程的文档。

请注意,不同的操作系统为其共享库使用不同的文件名后缀。Windows使用“.dll”,Mac使用“.dylib”,除mac以外的大多数unix使用“.so”。如果要使代码可移植,可以省略共享库文件名的后缀,并且sqlite3_load_extension()接口将自动添加适当的后缀。

还有一个SQL函数可用于加载扩展:load_extension(X,Y)。它的工作方式与sqlite3_load_extension()C接口类似。

这两种加载扩展的方法都允许您指定扩展的入口点的名称。您可以将此参数留空 - 为sqlite3_load_extension()C语言接口传递NULL指针或省略load_extension()SQL接口的第二个参数 - 并且扩展加载器逻辑将尝试自行计算入口点。它将首先尝试通用扩展名“sqlite3_extension_init”。如果这不起作用,它将使用模板“sqlite3_X_init”构造一个入口点,其中X由最后一个“/”之后和第一个后面的“。”之前的每个ASCII字符的小写等效替换。如果它们碰巧是“lib”,则省略前三个字符。所以,例如,如果文件名是“

出于安全原因,默认情况下会关闭扩展加载。为了使用C语言或SQL扩展加载函数,必须先在应用程序中使用sqlite3_db_config(db,SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION,1,NULL)C语言API启用扩展加载。

在命令行shell中,可以使用“.load”dot-command加载扩展。例如:

.load ./YourCode

请注意,命令行shell程序已经为您启用了扩展加载(通过调用sqlite3_enable_load_extension())接口作为其设置的一部分),因此上述命令无需任何特殊开关,设置或其他复杂操作即可运行。

带有一个参数的“.load”命令调用sqlite3_load_extension(),将zProc参数设置为NULL,从而导致SQLite首先查找名为“sqlite3_extension_init”的入口点,然后查找“x”源自文件名的“sqlite3_X_init”。如果您的扩展名具有不同名称的入口点,则只需提供该名称作为第二个参数即可。例如:

.load ./YourCode nonstandard_entry_point

可加载的扩展名是C代码。为了在大多数类Unix操作系统上编译它们,通常的命令是这样的:

gcc -g -fPIC -shared YourCode.c -o YourCode.so

Macs是类似unix的,但它们不遵循通常的共享库惯例。要在Mac上编译共享库,请使用如下命令:

gcc -g -fPIC -dynamiclib YourCode.c -o YourCode.dylib

如果当你尝试加载你的库时,你会得到一个错误消息,指出“mach-o,但是错误的体系结构”,那么你可能需要为gcc添加命令行选项“-arch i386”或“arch x86_64”,这取决于您的应用程序的构建方式。

要使用MSVC在Windows上编译,类似于以下内容的命令通常会起作用:

cl YourCode.c -link -dll -out:YourCode.dll

要使用MinGW编译Windows,命令行就像unix一样,只是输出文件后缀更改为“.dll”,省略了-fPIC参数:

gcc -g -shared YourCode.c -o YourCode.dll

模板可载入扩展包含以下三个元素:

  • #include <sqlite3ext.h>在源代码文件的顶部使用“ ”而不是“ #include <sqlite3.h>”。
  • SQLITE_EXTENSION_INIT1#include <sqlite3ext.h>行之后。
  • 添加一个扩展加载入口例程,看起来像下面这样:#ifdef _WIN32 __declspec(dllexport) #endif int sqlite3_extension_init( /* <== Change this name, maybe */ sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi ){ int rc = SQLITE_OK; SQLITE_EXTENSION_INIT2(pApi); /* insert code to initialize your extension here */ return rc; } You will do well to customize the name of your entry point to correspond to the name of the shared library you will be generating, rather than using the generic "sqlite3_extension_init" name. Giving your extension a custom entry point name will enable you to statically link two or more extensions into the same program without a linker conflict, if you later decide to use static linking rather than run-time linking. If your shared library ends up being named "YourCode.so" or "YourCode.dll" or "YourCode.dylib" as shown in the compiler examples above, then the correct entry point name would be "sqlite3_yourcode_init". Here is a complete template extension that you can copy/paste to get started:/* Add your header comment here */ #include <sqlite3ext.h> /* Do not use <sqlite3.h>! */ SQLITE_EXTENSION_INIT1 /* Insert your extension code here */ #ifdef _WIN32 __declspec(dllexport) #endif /* TODO: Change the entry point name so that "extension" is replaced by ** text derived from the shared library filename as follows: Copy every ** ASCII alphabetic character from the filename after the last "/" through ** the next following ".", converting each character to lowercase, and ** discarding the first three characters if they are "lib". */ int sqlite3_extension_init( sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi ){ int rc = SQLITE_OK; SQLITE_EXTENSION_INIT2(pApi); /* Insert here calls to ** sqlite3_create_function_v2(), ** sqlite3_create_collation_v2(), ** sqlite3_create_module_v2(), and/or ** sqlite3_vfs_register() ** to register the new features that your extension adds. */ return rc;} 4.1。示例扩展可以在SQLite源代码树中看到完整和可工作的可载入扩展的很多示例ext / misc子目录。该目录中的每个文件都是一个单独的扩展名。文档由文件头标注释提供。以下是关于ext / misc子目录中一些扩展的简要说明:
  • carray.c - 实现carray表值函数。
  • compress.c - 实现应用程序定义的SQL函数compress()和uncompress(),用于对文本或blob内容执行zLib压缩。
  • json1.c - 实现JSON SQL函数和表值函数。这是一个更大更复杂的扩展。
  • memvfs.c - 实现存储所有内容的新VFS。
  • rot13.c - 实现rot13() SQL函数。这是扩展函数的一个非常简单的例子,并且可以用作创建新扩展的模板。
  • series.c - 实现generate_series虚拟表和表值函数。这是一个虚拟表实现的相对简单的例子,它可以作为编写新虚拟表的模板。

其他更复杂的扩展可以在ext / ext / misc /以外的子文件夹中找到。

可加载扩展的默认行为是,当最初调用sqlite3_load_extension()的数据库连接关闭时,它将从进程内存中卸载。(换句话说,当数据库连接关闭时,会为所有扩展调用sqlite3_vfs对象的xDlUnload方法。)但是,如果初始化过程返回SQLITE_OK_LOAD_PERMANENTLY而不是SQLITE_OK,那么将不会卸载扩展(xDlClose不会被调用)并且扩展名将无限期地保留在进程内存中。对于想要注册新VFS的扩展,SQLITE_OK_LOAD_PERMANENTLY返回值很有用。

完全相同的源代码可以用于运行时可装入的共享库或DLL以及静态链接到您的应用程序的模块。这提供了灵活性,并允许您以不同的方式重用相同的代码。

要静态链接您的扩展,只需添加-DSQLITE_CORE编译时选项。SQLITE_CORE宏导致SQLITE_EXTENSION_INIT1和SQLITE_EXTENSION_INIT2宏变为空操作。然后修改你的应用程序直接调用入口点,传入一个NULL指针作为第三个“pApi”参数。

如果要静态链接两个或多个扩展名,使用基于扩展名文件名的入口点名称而非通用“sqlite3_extension_init”入口点名称尤其重要。如果使用通用名称,则会有多个相同符号的定义,并且链接将失败。

如果您将在您的应用程序中打开多个数据库连接,而不是分别为每个数据库连接调用扩展入口点,则可能需要考虑使用sqlite3_auto_extension()接口来注册您的扩展并使其自动启动数据库连接打开。你只需要注册每个扩展一次,并且你可以在你的main()例程的开始附近完成。使用sqlite3_auto_extension()接口来注册你的扩展使得你的扩展工作,就像它们被内置到核心SQLite中一样 - 只要你打开一个新的数据库连接而不需要被初始化,它们就会自动地在那里工作。确保在注册扩展之前完成使用sqlite3_config()完成的任何配置,

SQLite使用sqlite3_vfs对象的xDlOpen(),xDlError(),xDlSym()和xDlClose()方法实现运行时扩展加载。这些方法是使用unix上的dlopen()库实现的(这解释了为什么SQLite通常需要链接到unix系统上的“-ldl”库)并且在Windows上使用LoadLibrary()API。在一个不寻常的系统的自定义VFS中,这些方法都可以省略,在这种情况下,运行时扩展加载机制将不起作用(尽管您仍然可以静态链接扩展代码,假设条目指针是唯一命名的) 。可以使用SQLITE_OMIT_LOAD_EXTENSION编译SQLite以忽略构建中的扩展加载代码。

 SQLite is in the Public Domain.

其他 | Miscellaneous相关

1.35% Faster Than The Filesystem
2.8+3 Filenames
3.An Asynchronous I/O Module For SQLite
4.Appropriate Uses For SQLite
5.Architecture of SQLite
6.Atomic Commit In SQLite
7.Automatic Undo/Redo With SQLite
8.Benefits of SQLite As A File Format
9.Change in Default Page Size in SQLite Version 3.12.0
10.Clustered Indexes and the WITHOUT ROWID Optimization
11.Compile-time Options
12.Constraint Conflict Resolution in SQLite
13.Custom Builds Of SQLite
14.Deterministic SQL Functions
15.Distinctive Features Of SQLite
16.EXPLAIN QUERY PLAN
17.Features Of SQLite
18.File Format Changes in SQLite
19.Full-Featured SQL
20.High Reliability
21.Hints for Debugging SQLite
22.How SQLite Is Tested
23.How To Compile SQLite
24.How To Download Canonical SQLite Source Code
25.Imposter Tables
26.In-Memory Databases
27.Indexes On Expressions
28.Internal Versus External BLOBs
29.Isolation In SQLite
30.Long Term Support
31.Maintaining Private Branches Of SQLite
32.Many Small Queries Are Efficient In SQLite
33.Measuring and Reducing CPU Usage in SQLite
34.Memory-Mapped I/O
35.NULL Handling in SQLite
36.Partial Indexes
37.Pointer Passing Interfaces
38.Powersafe Overwrite
39.Release History Of SQLite
40.Result and Error Codes
41.Row Values
42.Rowid Tables
43.SQL Features That SQLite Does Not Implement
44.sqldiff.exe: Database Difference Utility
45.SQLite As An Application File Format
46.SQLite Autoincrement
47.SQLite Backup API
48.SQLite Changes From Version 3.4.2 To 3.5.0
49.SQLite Changes From Version 3.5.9 To 3.6.0
50.SQLite Database Speed Comparison
51.SQLite File IO Specification
52.SQLite Frequently Asked Questions
53.SQLite In 5 Minutes Or Less
54.SQLite is a Self Contained System
55.SQLite Is Serverless
56.SQLite Is Transactional
57.SQLite Library Footprint
58.SQLite Shared-Cache Mode
59.SQLite Unlock-Notify API
60.SQLite Version 3 Overview
61.SQLite: Single File Database
62.Temporary Files Used By SQLite
63.TH3
64.The COMPLETION() Table-Valued Function
65.The CSV Virtual Table
66.The dbhash.exe Utility Program
67.The DBSTAT Virtual Table
68.The Error And Warning Log
69.The generate_series Table-Valued Function
70.The OS Backend (VFS) To SQLite
71.The Spellfix1 Virtual Table
72.The SQLite Amalgamation
73.The SQLite Bytecode Engine
74.The sqlite3_analyzer.exe Utility Program
75.The SQLITE_STMT Virtual Table
76.The UNION Virtual Table
77.The Virtual Database Engine of SQLite
78.Uniform Resource Identifiers
79.Using SQLite In Multi-Threaded Applications
80.Version Numbers in SQLite
81.What If OpenDocument Used SQLite?
82.Why Is SQLite Coded In C
83.Zero-Configuration
Sqlite

SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中。它是D.RichardHipp建立的公有领域项目。它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了。它能够支持Windows/Linux/Unix等等主流的操作系统,同时能够跟很多程序语言相结合,比如 Tcl、C#、PHP、Java等,还有ODBC接口,同样比起Mysql、PostgreSQL这两款开源的世界著名数据库管理系统来

主页 https://sqlite.org/
源码 https://www.sqlite.org/src/
发布版本 3.21.0

Sqlite目录

1.C界面 | C Interface
2.C Interface: Session Module
3.CLI
4.数据库文件表 | Database File Format
5.数据类 | Datatypes
6.动态内存分配 | Dynamic Memory Allocation
7.外键约束 | Foreign Key Constraints
8.全文索引 | Full-Text Search
9.损坏方式 | How To Corrupt
10.JSON
11.语言 | Language
12.局限性 | Limits
13.锁定和并发 | Locking and Concurrency
14.其他 | Miscellaneous
15.PRAGMA Statements
16.查询计划程序 | Query Planner
17.R*Tree Module
18.RBU Extension
19.语法图 | Syntax Diagrams
20.Tcl Interface
21.虚拟表机制 | Virtual Table Mechanism
22.预写日志 | Write-Ahead Logging
23.SQL 教程
24.SQL 简介
25.SQL 语法
26.SQL DELETE 语句
27.SQL UPDATE 语句
28.SQL NOT NULL 约束
29.SQL 约束
30.SQL CREATE TABLE 语句
31.SQL CREATE DATABASE 语句
32.SQL INSERT INTO SELECT 语句
33.SQL SELECT INTO 语句
34.SQL CREATE VIEW、REPLACE VIEW、 DROP VIEW 语句
35.SQL AUTO INCREMENT 字段
36.SQL ALTER TABLE 语句
37.SQL 撤销索引、表以及数据库
38.SQL CREATE INDEX 语句
39.SQL DEFAULT 约束
40.SQL CHECK 约束
41.SQL FOREIGN KEY 约束
42.SQL PRIMARY KEY 约束
43.SQL UNIQUE 约束
44.SQL 通用数据类型
45.SQL ISNULL()、NVL()、IFNULL() 和 COALESCE() 函数
46.SQL NULL 值 – IS NULL 和 IS NOT NULL
47.SQL Server 和 MySQL 中的 Date 函数
48.SQL MS Access、MySQL 和 SQL Server 数据类型
49.SQL 函数
50.SQL 总结
51.SQL 主机
52.SQL 快速参考
53.SQL ROUND() 函数
54.SQL Server GETDATE() 函数
55.MySQL DATE_FORMAT() 函数
56.MySQL DATEDIFF() 函数
57.MySQL DATE_SUB() 函数
58.MySQL DATE_ADD() 函数
59.MySQL EXTRACT() 函数
60.MySQL DATE() 函数
61.MySQL CURTIME() 函数
62.MySQL CURDATE() 函数
63.MySQL NOW() 函数
64.SQL Server CONVERT() 函数
65.SQL Server DATEDIFF() 函数
66.SQL Server DATEADD() 函数
67.SQL Server DATEPART() 函数
68.SQLite 命令
69.SQLite 安装
70.SQLite 简介
71.SQLite 运算符
72.SQLite Select 语句
73.SQLite 删除表
74.SQLite 创建表
75.SQLite Insert 语句
76.SQLite 分离数据库
77.SQLite 附加数据库
78.SQLite 创建数据库
79.SQLite 数据类型
80.SQLite 语法
81.SQLite Order By
82.SQLite Limit 子句
83.SQLite Glob 子句
84.SQLite Like 子句
85.SQLite Delete 语句
86.SQLite Update 语句
87.SQLite AND/OR 运算符
88.SQLite Where 子句
89.SQLite 表达式
90.SQLite Distinct 关键字
91.SQLite Having 子句
92.SQLite Group By
93.SQLite Join
94.SQLite 约束
95.SQLite PRAGMA
96.SQLite 事务
97.SQLite 视图
98.SQLite Truncate Table
99.SQLite Alter 命令
100.SQLite Indexed By