如何使用保留字节创建数据库? [英] how to create database with reserved bytes?

查看:24
本文介绍了如何使用保留字节创建数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在 SQLite 数据库中的每页末尾保留字节.

It is possible to reserve bytes at the end of each page in a SQLite database.

根据文档:

SQLite 能够在每个页面的末尾留出少量额外字节供扩展使用.例如,SQLite 加密扩展使用这些额外的字节来存储与每个页面相关联的随机数和/或加密校验和.偏移量 20 处的 1 字节整数中的保留空间"大小是每页末尾为扩展保留的空间字节数.该值通常为 0.该值可以是奇数.

SQLite has the ability to set aside a small number of extra bytes at the end of every page for use by extensions. These extra bytes are used, for example, by the SQLite Encryption Extension to store a nonce and/or cryptographic checksum associated with each page. The "reserved space" size in the 1-byte integer at offset 20 is the number of bytes of space at the end of each page to reserve for extensions. This value is usually 0. The value can be odd.

我想使用此功能并尝试创建一个普通的空数据库并手动将字节(标题,偏移量为 20;使用十六进制编辑器)更改为非空值(-> x10 表示十六个保留字节).

I want to use this feature and tried it with creating a normal, empty database and manually changed the byte (header, at offset 20; used a hex editor) to a non-null value (-> x10 for sixteen reserved bytes).

在 sqlite3 中使用 .dbinfo:

Using .dbinfo in sqlite3:

(…) 保留字节:16 (…)

(…) reserved bytes: 16 (…)

所以一切似乎都是正确的,但是没有任何编辑数据库的命令会成功.每个命令(例如 CREATE TABLE example('id' int);)都会导致此错误消息:

So everything seems to be correct, but no command to edit the database will be successful. Each command (e.g. CREATE TABLE example('id' int);) results in this error message:

错误:数据库磁盘映像格式错误

Error: database disk image is malformed

所以我的问题是:如何使用此保留空间(不会因格式错误而被拒绝)创建 SQLite 数据库?

So my question is: How can I create a SQLite database with this reserved space (which will not be rejected as malformed)?

推荐答案

第一个数据库页面不仅包含标题,还包含第一个 B 树页面.您必须确保没有任何数据指向页面的可用区域之外.如果主表为空,唯一需要调整的是指向 B 树标题偏移 5 处单元格内容区域开头的指针(在这种情况下,它应该简单地指向可用区域的结尾).

The first database page contains not only the header, but also the first B-tree page of the sqlite_master table. You have to ensure that none of that data points to outside of the usable area of the page. If the master table is empty, the only thing to adjust is the pointer to the start of the cell content area at offset 5 of the B-tree header (which in this case should simply point to the end of the usable area).

无论如何,如果 SQLite 库不是使用 SQLITE_OMIT_BUILTIN_TEST 编译的,您可以使用sqlite3_test_control() 函数设置此值(对于空数据库):

Anyway, if the SQLite library was not compiled with SQLITE_OMIT_BUILTIN_TEST, you can use the sqlite3_test_control() function to set this value (for an empty database):

sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N)

这篇关于如何使用保留字节创建数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆