()
| 47 | |
| 48 | |
| 49 | def main(): |
| 50 | import io |
| 51 | import pprint |
| 52 | |
| 53 | # Initialize and populate our database. |
| 54 | conn = sqlite3.connect(":memory:") |
| 55 | cursor = conn.cursor() |
| 56 | cursor.execute("CREATE TABLE memos(key INTEGER PRIMARY KEY, task TEXT)") |
| 57 | tasks = ( |
| 58 | 'give food to fish', |
| 59 | 'prepare group meeting', |
| 60 | 'fight with a zebra', |
| 61 | ) |
| 62 | for task in tasks: |
| 63 | cursor.execute("INSERT INTO memos VALUES(NULL, ?)", (task,)) |
| 64 | |
| 65 | # Fetch the records to be pickled. |
| 66 | cursor.execute("SELECT * FROM memos") |
| 67 | memos = [MemoRecord(key, task) for key, task in cursor] |
| 68 | # Save the records using our custom DBPickler. |
| 69 | file = io.BytesIO() |
| 70 | DBPickler(file).dump(memos) |
| 71 | |
| 72 | print("Pickled records:") |
| 73 | pprint.pprint(memos) |
| 74 | |
| 75 | # Update a record, just for good measure. |
| 76 | cursor.execute("UPDATE memos SET task='learn italian' WHERE key=1") |
| 77 | |
| 78 | # Load the records from the pickle data stream. |
| 79 | file.seek(0) |
| 80 | memos = DBUnpickler(file, conn).load() |
| 81 | |
| 82 | print("Unpickled records:") |
| 83 | pprint.pprint(memos) |
| 84 | |
| 85 | |
| 86 | if __name__ == '__main__': |
no test coverage detected
searching dependent graphs…