This document is for an old version of Python that is no longer supported. You should upgrade and read the Python 当前稳定版本的文档.

# 11. 数据持久化

The modules described in this chapter support storing Python data in a persistent form on disk. The `pickle` and `marshal` modules can turn many Python data types into a stream of bytes and then recreate the objects from the bytes. The various DBM-related modules support a family of hash-based file formats that store a mapping of strings to other strings. The `bsddb` module also provides such disk-based string-to-string mappings based on hashing, and also supports B-Tree and record-based formats.

本章中描述的模块列表是：

- 11.1. `pickle` —— Python 对象序列化
  - 11.1.1. 与其他 Python 模块间的关系
  - 11.1.2. 数据流格式
  - 11.1.3. Usage
  - 11.1.4. 可以被打包/解包的对象
  - 11.1.5. The pickle protocol
    - 11.1.5.1. Pickling and unpickling normal class instances
    - 11.1.5.2. Pickling and unpickling extension types
    - 11.1.5.3. Pickling and unpickling external objects
  - 11.1.6. Subclassing Unpicklers
  - 11.1.7. Example
- 11.2. `cPickle` — A faster `pickle`
- 11.3. `copy_reg` — Register `pickle` support functions
  - 11.3.1. Example
- 11.4. `shelve` — Python 对象持久化
  - 11.4.1. 限制
  - 11.4.2. 示例
- 11.5. `marshal` — 内部 Python 对象序列化
- 11.6. `anydbm` — Generic access to DBM-style databases
- 11.7. `whichdb` — Guess which DBM module created a database
- 11.8. `dbm` — Simple “database” interface
- 11.9. `gdbm` — GNU’s reinterpretation of dbm
- 11.10. `dbhash` — DBM-style interface to the BSD database library
  - 11.10.1. Database Objects
- 11.11. `bsddb` — Interface to Berkeley DB library
  - 11.11.1. Hash, BTree and Record Objects
- 11.12. `dumbdbm` — Portable DBM implementation
  - 11.12.1. Dumbdbm Objects
- 11.13. `sqlite3` — SQLite 数据库 DB-API 2.0 接口模块
  - 11.13.1. 模块函数和常量
  - 11.13.2. 连接对象（Connection）
  - 11.13.3. Cursor 对象
  - 11.13.4. 行对象*Row*
  - 11.13.5. SQLite 与 Python 类型
    - 11.13.5.1. 概述
    - 11.13.5.2. 使用适配器将额外的 Python 类型保存在 SQLite 数据库中。
      - 11.13.5.2.1. 让对象自行调整
      - 11.13.5.2.2. 注册可调用的适配器
    - 11.13.5.3. 将SQLite 值转换为自定义Python 类型
    - 11.13.5.4. 默认适配器和转换器
  - 11.13.6. 控制事务
  - 11.13.7. 有效使用 `sqlite3`
    - 11.13.7.1. 使用快捷方式
    - 11.13.7.2. 通过名称而不是索引访问索引
    - 11.13.7.3. 使用连接作为上下文管理器
  - 11.13.8. 常见问题
    - 11.13.8.1. 多线程