非常教程

Sqlite参考手册

其他 | Miscellaneous

SQLite Backup API

从历史上看,使用以下方法创建SQLite数据库的备份(副本):

  • 使用SQLite API(即shell工具)在数据库文件上建立一个共享锁。
  • 使用外部工具(例如unix'cp'实用程序或DOS'copy'命令)复制数据库文件。
  • 放弃步骤1中获取的数据库文件的共享锁。

这个程序在许多情况下运作良好,通常速度非常快。但是,这种技术有以下缺点:

  • 任何希望在创建备份时写入数据库文件的数据库客户端都必须等到共享锁被放弃。
  • 它不能用于将数据复制到内存数据库或从内存数据库复制数据。
  • 如果复制数据库文件时发生电源故障或操作系统故障,系统恢复后备份数据库可能会损坏。

在线备份API是为了解决这些问题而创建的。联机备份API允许将一个数据库的内容复制到另一个数据库中,覆盖目标数据库的原始内容。复制操作可以逐步完成,在这种情况下,源数据库不需要在复制过程中被锁定,仅在真正被读取的短暂时间段内被锁定。这允许其他数据库用户在进行在线数据库的备份时不中断地继续。

这里记录了在线备份API。本页面的其余部分包含两个C语言示例,说明API的常见用法及其讨论。阅读这些示例不能替代阅读API文档!

Example 1: Loading and Saving In-Memory Databases

/*
** This function is used to load the contents of a database file on disk 
** into the "main" database of open database connection pInMemory, or
** to save the current contents of the database opened by pInMemory into
** a database file on disk. pInMemory is probably an in-memory database, 
** but this function will also work fine if it is not.
**
** Parameter zFilename points to a nul-terminated string containing the
** name of the database file on disk to load from or save to. If parameter
** isSave is non-zero, then the contents of the file zFilename are 
** overwritten with the contents of the database opened by pInMemory. If
** parameter isSave is zero, then the contents of the database opened by
** pInMemory are replaced by data loaded from the file zFilename.
**
** If the operation is successful, SQLITE_OK is returned. Otherwise, if
** an error occurs, an SQLite error code is returned.
*/
int loadOrSaveDb(sqlite3 *pInMemory, const char *zFilename, int isSave){
  int rc;                   /* Function return code */
  sqlite3 *pFile;           /* Database connection opened on zFilename */
  sqlite3_backup *pBackup;  /* Backup object used to copy data */
  sqlite3 *pTo;             /* Database to copy to (pFile or pInMemory) */
  sqlite3 *pFrom;           /* Database to copy from (pFile or pInMemory) */

  /* Open the database file identified by zFilename. Exit early if this fails
  ** for any reason. */
  rc = sqlite3_open(zFilename, &pFile);
  if( rc==SQLITE_OK ){

    /* If this is a 'load' operation (isSave==0), then data is copied
    ** from the database file just opened to database pInMemory. 
    ** Otherwise, if this is a 'save' operation (isSave==1), then data
    ** is copied from pInMemory to pFile.  Set the variables pFrom and
    ** pTo accordingly. */
    pFrom = (isSave ? pInMemory : pFile);
    pTo   = (isSave ? pFile     : pInMemory);

    /* Set up the backup procedure to copy from the "main" database of 
    ** connection pFile to the main database of connection pInMemory.
    ** If something goes wrong, pBackup will be set to NULL and an error
    ** code and message left in connection pTo.
    **
    ** If the backup object is successfully created, call backup_step()
    ** to copy data from pFile to pInMemory. Then call backup_finish()
    ** to release resources associated with the pBackup object.  If an
    ** error occurred, then an error code and message will be left in
    ** connection pTo. If no error occurred, then the error code belonging
    ** to pTo is set to SQLITE_OK.
    */
    pBackup = sqlite3_backup_init(pTo, "main", pFrom, "main");
    if( pBackup ){
      (void)sqlite3_backup_step(pBackup, -1);
      (void)sqlite3_backup_finish(pBackup);
    }
    rc = sqlite3_errcode(pTo);
  }

  /* Close the database connection opened on database file zFilename
  ** and return the result of this function. */
  (void)sqlite3_close(pFile);
  return rc;
}

右侧的C函数演示了备份API最简单和最常见的用法之一:将内存数据库的内容加载并保存到磁盘上的文件。在这个例子中使用备份API如下:

  • 调用函数sqlite3_backup_init()以创建sqlite3_backup对象,以在两个数据库之间复制数据(从文件到内存数据库或反之亦然)。
  • 函数sqlite3_backup_step()被调用,其参数是-1将整个源数据库复制到目标。
  • 函数sqlite3_backup_finish()被调用来清理由sqlite3_backup_init()分配的资源。

Error handling

如果三个主要备份API例程中的任何一个发生错误,则错误代码和消息将附加到目标数据库连接。此外,如果sqlite3_backup_step()遇到错误,则错误代码由sqlite3_backup_step()调用本身以及随后对sqlite3_backup_finish()的调用返回。因此,对sqlite3_backup_finish()的调用不会通过sqlite3_backup_step()覆盖存储在目标数据库连接中的错误代码。示例代码使用此功能来减少所需的错误处理量。sqlite3_backup_step()和sqlite3_backup_finish()调用的返回值将被忽略,并且错误代码表示从目标数据库连接收集的复制操作成功或失败。

Possible Enhancements

至少可以通过以下两种方式来增强该功能的实施:

  • 无法获取数据库文件zFilename上的锁定(SQLITE_BUSY错误),并且
  • 数据库pInMemory和zFilename的页面大小不同的情况可以更好地处理。

由于数据库zFilename是磁盘上的文件,因此它可能由另一个进程在外部访问。这意味着,当对sqlite3_backup_step()的调用尝试读取或写入数据时,它可能无法获得所需的文件锁定。如果发生这种情况,这个实现将失败,立即返回SQLITE_BUSY。解决方法是在打开数据库连接pFile后立即使用sqlite3_busy_handler()或sqlite3_busy_timeout()注册忙处理程序回调或超时。如果它无法立即获得所需的锁,sqlite3_backup_step()将以与sqlite3_step()或sqlite3_exec()相同的方式使用任何已注册的繁忙处理程序回调或超时。

通常,在覆盖目标的内容之前,源数据库和目标数据库的页面大小是否不同,无关紧要。目标数据库的页面大小仅作为备份操作的一部分进行更改。如果目标数据库碰巧是内存数据库,则是例外。在这种情况下,如果页面大小在备份操作开始时不相同,则该操作将失败并显示SQLITE_READONLY错误。不幸的是,使用函数loadOrSaveDb()将数据库中的数据库映像加载到内存数据库时可能会发生这种情况。

但是,如果内存数据库pInMemory在被传递到函数loadOrSaveDb()之前刚被打开(因此完全为空),那么仍然可以使用SQLite“PRAGMA page_size”命令更改其页面大小。函数loadOrSaveDb()可以检测到这种情况,并尝试在调用联机备份API函数之前将内存数据库的页面大小设置为数据库zFilename的页大小。

Example 2: Online Backup of a Running Database

/*
** Perform an online backup of database pDb to the database file named
** by zFilename. This function copies 5 database pages from pDb to
** zFilename, then unlocks pDb and sleeps for 250 ms, then repeats the
** process until the entire database is backed up.
** 
** The third argument passed to this function must be a pointer to a progress
** function. After each set of 5 pages is backed up, the progress function
** is invoked with two integer parameters: the number of pages left to
** copy, and the total number of pages in the source file. This information
** may be used, for example, to update a GUI progress bar.
**
** While this function is running, another thread may use the database pDb, or
** another process may access the underlying database file via a separate 
** connection.
**
** If the backup process is successfully completed, SQLITE_OK is returned.
** Otherwise, if an error occurs, an SQLite error code is returned.
*/
int backupDb(
  sqlite3 *pDb,               /* Database to back up */
  const char *zFilename,      /* Name of file to back up to */
  void(*xProgress)(int, int)  /* Progress function to invoke */     
){
  int rc;                     /* Function return code */
  sqlite3 *pFile;             /* Database connection opened on zFilename */
  sqlite3_backup *pBackup;    /* Backup handle used to copy data */

  /* Open the database file identified by zFilename. */
  rc = sqlite3_open(zFilename, &pFile);
  if( rc==SQLITE_OK ){

    /* Open the sqlite3_backup object used to accomplish the transfer */
    pBackup = sqlite3_backup_init(pFile, "main", pDb, "main");
    if( pBackup ){

      /* Each iteration of this loop copies 5 database pages from database
      ** pDb to the backup database. If the return value of backup_step()
      ** indicates that there are still further pages to copy, sleep for
      ** 250 ms before repeating. */
      do {
        rc = sqlite3_backup_step(pBackup, 5);
        xProgress(
            sqlite3_backup_remaining(pBackup),
            sqlite3_backup_pagecount(pBackup)
        );
        if( rc==SQLITE_OK || rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
          sqlite3_sleep(250);
        }
      } while( rc==SQLITE_OK || rc==SQLITE_BUSY || rc==SQLITE_LOCKED );

      /* Release resources allocated by backup_init(). */
      (void)sqlite3_backup_finish(pBackup);
    }
    rc = sqlite3_errcode(pFile);
  }
  
  /* Close the database connection opened on database file zFilename
  ** and return the result of this function. */
  (void)sqlite3_close(pFile);
  return rc;
}

前面示例中提供的函数将整个源数据库在一次调用中复制到sqlite3_backup_step()。这需要在操作期间对源数据库文件持有读锁定,以防止其他数据库用户写入数据库。它还在整个副本中保存与数据库pInMemory关联的互斥锁,以防止其他线程使用它。本节中的C函数被设计为由后台线程或进程调用以创建在线数据库的备份,从而通过以下方法避免了这些问题:

  • 调用函数sqlite3_backup_init()以创建sqlite3_backup对象,以便将数据库pDb中的数据复制到由zFilename标识的备份数据库文件。
  • 使用参数5调用函数sqlite3_backup_step()以将5页数据库pDb复制到备份数据库(文件zFilename)。
  • 如果还有更多页面要从数据库pDb中复制,那么函数将休眠250毫秒(使用sqlite3_sleep()实用程序),然后返回到步骤2。
  • 函数sqlite3_backup_finish()被调用来清理由sqlite3_backup_init()分配的资源。

File and Database Connection Locking

在上述步骤3中的250 ms睡眠期间,数据库文件上没有读锁,并且与pDb关联的互斥锁未保持。这允许其他线程使用数据库连接pDb和其他连接来写入底层数据库文件。

如果在此函数处于休眠状态时另一个线程或进程将数据写入源数据库,则SQLite会检测到此情况,并且通常会在下一次调用sqlite3_backup_step()时重新启动备份进程。此规则有一个例外:如果源数据库不是内存数据库,并且写操作是在与备份操作相同的进程内执行的并使用相同的数据库句柄(pDb),则目标数据库(一个使用连接pFile打开)会随源一起自动更新。在sqlite3_sleep()调用返回后,备份过程可能会继续,如同没有任何事情发生一样。

无论备份过程是否由于写入源数据库中间备份而重新启动,用户都可以确保备份操作完成后,备份数据库将包含原始数据的一致和最新快照。然而:

  • 写入内存源数据库或通过外部进程或线程使用除pDb以外的数据库连接写入基于文件的源数据库比使用pDb对基于文件的源数据库写入要昂贵得多(如整个备份操作必须在前两种情况下重新启动)。
  • 如果备份进程足够频繁地重新启动,它可能永远不会运行完成,并且backupDb()函数可能永远不会返回。

backup_remaining() and backup_pagecount()

backupDb()函数使用sqlite3_backup_remaining()和sqlite3_backup_pagecount()函数通过用户提供的xProgress()回调报告其进度。函数sqlite3_backup_remaining()返回要复制的页数,sqlite3_backup_pagecount()返回源数据库中的页面总数(在本例中为由pDb打开的数据库)。因此,该过程的完成百分比可以计算为:

Completion = 100% * (pagecount() - remaining()) / pagecount()

sqlite3_backup_remaining()和sqlite3_backup_pagecount()API将先前调用sqlite3_backup_step()的值存储起来,但它们实际上并不检查源数据库文件。这意味着如果在调用sqlite3_backup_step()之后但在使用sqlite3_backup_remaining()和sqlite3_backup_pagecount()返回的值之前,源数据库由另一个线程或进程写入,则这些值在技术上可能不正确。这通常不是问题。

 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.Run-Time Loadable Extensions
44.SQL Features That SQLite Does Not Implement
45.sqldiff.exe: Database Difference Utility
46.SQLite As An Application File Format
47.SQLite Autoincrement
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