diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2017-06-11 17:44:11 +0300 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2017-06-11 17:44:11 +0300 |
commit | 6ca086a89a86ab9b01ad8e1ec4621996b08f06e0 (patch) | |
tree | 7a6a8eb2e60272e7321cc67f2f62eb8f0bb0f07b /docs/library | |
parent | 869cdcfdfc860b1177baf9b0f8818915ba54f3f5 (diff) | |
download | micropython-6ca086a89a86ab9b01ad8e1ec4621996b08f06e0.tar.gz micropython-6ca086a89a86ab9b01ad8e1ec4621996b08f06e0.zip |
docs/btree: Add hints about opening db file and need to flush db.
Diffstat (limited to 'docs/library')
-rw-r--r-- | docs/library/btree.rst | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/docs/library/btree.rst b/docs/library/btree.rst index bd7890586a..7108fba430 100644 --- a/docs/library/btree.rst +++ b/docs/library/btree.rst @@ -23,7 +23,13 @@ Example:: # First, we need to open a stream which holds a database # This is usually a file, but can be in-memory database # using uio.BytesIO, a raw flash section, etc. - f = open("mydb", "w+b") + # Oftentimes, you want to create a database file if it doesn't + # exist and open if it exists. Idiom below takes care of this. + # DO NOT open database with "a+b" access mode. + try: + f = open("mydb", "r+b") + except OSError: + f = open("mydb", "w+b") # Now open a database itself db = btree.open(f) @@ -33,6 +39,11 @@ Example:: db[b"1"] = b"one" db[b"2"] = b"two" + # Assume that any changes are cached in memory unless + # explicitly flushed (or database closed). Flush database + # at the end of each "transaction". + db.flush() + # Prints b'two' print(db[b"2"]) |