Back to index

python3.2  3.2.2
Classes | Functions | Variables
dbpickle Namespace Reference

Classes

class  DBPickler
class  DBUnpickler

Functions

def main

Variables

tuple MemoRecord = namedtuple("MemoRecord", "key, task")

Function Documentation

def dbpickle.main ( void  )

Definition at line 49 of file dbpickle.py.

00049 
00050 def main():
00051     import io
00052     import pprint
00053 
00054     # Initialize and populate our database.
00055     conn = sqlite3.connect(":memory:")
00056     cursor = conn.cursor()
00057     cursor.execute("CREATE TABLE memos(key INTEGER PRIMARY KEY, task TEXT)")
00058     tasks = (
00059         'give food to fish',
00060         'prepare group meeting',
00061         'fight with a zebra',
00062         )
00063     for task in tasks:
00064         cursor.execute("INSERT INTO memos VALUES(NULL, ?)", (task,))
00065 
00066     # Fetch the records to be pickled.
00067     cursor.execute("SELECT * FROM memos")
00068     memos = [MemoRecord(key, task) for key, task in cursor]
00069     # Save the records using our custom DBPickler.
00070     file = io.BytesIO()
00071     DBPickler(file).dump(memos)
00072 
00073     print("Pickled records:")
00074     pprint.pprint(memos)
00075 
00076     # Update a record, just for good measure.
00077     cursor.execute("UPDATE memos SET task='learn italian' WHERE key=1")
00078 
00079     # Load the records from the pickle data stream.
00080     file.seek(0)
00081     memos = DBUnpickler(file, conn).load()
00082 
00083     print("Unpickled records:")
00084     pprint.pprint(memos)
00085 

Here is the call graph for this function:


Variable Documentation

tuple dbpickle.MemoRecord = namedtuple("MemoRecord", "key, task")

Definition at line 9 of file dbpickle.py.