Databases and Persistence
Persistent data is the kind of data that outlives a program that creates it.
How to save information in between program executions?
1. Flat files: Text and bytes stored directly in the computer.
2. DBM keyed files: Keyed access to strings stored in dictionary-like files
3. Pickled objects: Serialized Python objects saved to files and streams
4. Shelve files: Pickled Python objects saved in DBM keyed files.
5. Object-oriented databases(OODBs): Persistent Python objects stored in persistent dictionaries(ZODB, Durus)
6. SQL relational databases(RDBMSs): Table-based storage that supports SQL queries(SQLite, MySQL, PostGreSQL etc)
7. Object relational mappers(ORMs): Mediators that map Python classes to relational tables(SQLObject, SQLAlchemy)
Flat files
Flat files are handy for simple persistence tasks, but they are generally geared toward a sequential processing mode.
Although it is possible to jump around to arbitrary locations with ‘seek’ calls, flat files don’t provide much structure to data beyond the notion of bytes and text lines.
DBM files
- Information can be stored in a DBM file using unique key.(Technically DBM files generally use a technique called hashing to store data in files)
- DBM is one of the easiest ways to save information in Python since they behave so much like in-memory dictionaries.
- In a DBM file object, indexing by key fetches data from the file. Assigning to an index stores data in the file.