【sqlite】一条简单插入的页面变化

c++代码如下:

cpp 复制代码
#include <stdio.h>
#include "sqlite3.h"

int main() {
    sqlite3* db;
    char* err_msg = NULL;
    int rc;

    // 删除之前的测试数据库(如果存在)
    remove("int.db");

    // 打开数据库连接
    rc = sqlite3_open("int.db", &db);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "无法打开数据库: %s\n", sqlite3_errmsg(db));
        return 1;
    }
    printf("1. 数据库连接已建立\n");

    // 创建测试表
    const char* create_table_sql =
        "CREATE TABLE test_ints ("
            "tiny INT,"
            "small INT, "
            "medium INT,"
            "large INT,"
            "negative INT"
        ")";

    rc = sqlite3_exec(db, create_table_sql, NULL, NULL, &err_msg);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "创建表失败: %s\n", err_msg);
        sqlite3_free(err_msg);
        return 1;
    }
    printf("2. 测试表已创建\n");

    // 开始一个事务(显式开始)
    rc = sqlite3_exec(db, "BEGIN TRANSACTION;", NULL, NULL, &err_msg);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "开始事务失败: %s\n", err_msg);
        sqlite3_free(err_msg);
        return 1;
    }
    printf("3. 事务已开始\n");

    // 插入数据但不提交
    const char* insert_sql =
        "INSERT INTO test_ints VALUES (1, 128, 32768, 2147483647, -1);"
        "INSERT INTO test_ints VALUES (127, 255, 65535, 4294967295, -128);"
        "INSERT INTO test_ints VALUES (0, 16384, 1000000, 1000000000, -32768);";

    rc = sqlite3_exec(db, insert_sql, NULL, NULL, &err_msg);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "插入数据失败: %s\n", err_msg);
        sqlite3_free(err_msg);
        return 1;
    }
    printf("4. 数据已插入但未提交\n");

    // 提交事务
    rc = sqlite3_exec(db, "COMMIT;", NULL, NULL, &err_msg);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "提交事务失败: %s\n", err_msg);
        sqlite3_free(err_msg);
    }
    else {
        printf("\n6. 事务已提交\n");
    }

    sqlite3_close(db);

    return 0;
}

运行后,int.db总共有2个页面。第2个页面的末尾有如下数据:

00 00 00 00 00 00 11 03 06 08 02 03 04 02 40 00

0F 42 40 3B 9A CA 00 80 00 13 02 06 01 02 03 05

01 7F 00 FF 00 FF FF 00 00 FF FF FF FF 80 10 01

06 09 02 03 04 01 00 80 00 80 00 7F FF FF FF FF

分析如下:

00 00 00 00 00 00

(第三条)

11 03 06 08 02 03 04 02

40 00 (16384)

0F 42 40 (1000000)

3B 9A CA 00 (1000000000)

80 00 (-32768)

(第二条)

13 02 06 01 02 03 05 01

7F 00 (127)

FF 00 (255)

FF FF 00 00 (65535)

FF FF FF FF (4294967295)

80 (-128)

(第一条)

10 01 06 09 02 03 04

01 (1)

80 (128)

00 80 00 (32768)

7F FF FF FF (2147483647)

FF (-1)

每一条的最前面是1字节的nPayload和6字节的Hdr(头部)。

nPayload等于nByte + nZero(自动补零的数量),这里后者为0,前者则等于nHdr+nData。

这3条的nHdr都是6。

nData则分别是10、13、11.相加最终就得到了16, 19, 17。

详见操作符MakeRecord,

uu nData
<=127(哪怕为负) 为0则直接不加,否则+=1
<=32767(哪怕为负) +=2
<=8388607(哪怕为负) +=3
<=2147483647(哪怕为负) +=4
<=140737488355327LL(哪怕为负) +=6
else +=8

这里我注意到插入-32768的时候uu是按照32767计算的,而插入-128的时候依旧是-128。暂不清楚原因。

相关推荐
百锦再5 天前
Django实现接口token检测的实现方案
数据库·python·django·sqlite·flask·fastapi·pip
kangzerun5 天前
SQLiteManager:一个优雅的Qt SQLite数据库操作类
数据库·qt·sqlite
用户5757303346245 天前
AIGC 时代数据库革命:告别手写 SQL,用自然语言驾驭 SQLite
sqlite
GDAL6 天前
better-sqlite3 深度教程:为什么它比 node‑sqlite3 更好、怎么用、什么时候不该用
sqlite·better-sqlite3
GDAL6 天前
SQLite 与 MySQL 性能深度对比:场景决定最优解
数据库·mysql·sqlite
钱彬 (Qian Bin)7 天前
FastAPI的Alembic踩坑记录:缺失历史迁移脚本如何保留数据重建版本控制
sqlite·fastapi·数据库迁移·alembic
qq_454245038 天前
GraphMindStudio 数据操作层解析:基于 SQLite 的封装与自动化存储
sqlite·c#
喵手8 天前
Python爬虫实战:Boss直聘职位数据采集实战 - Playwright + 结构化解析完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·sqlite·爬虫实战·playwright·boss直聘职位数据采集·结构化解析
GDAL9 天前
SQLite 的适用场景与选型指南:它不是轻量 MySQL,而是「文件的升级版」
数据库·mysql·sqlite
布局呆星9 天前
Python 入门:FastAPI + SQLite3 + Requests 基础教学
python·sqlite·fastapi