Advertisement
lessientelrunya

databases

Apr 7th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 KB | None | 0 0
  1. >>> import anydbm
  2. >>> db = anydbm.open('captions.db', 'c')
  3.  
  4. # The mode 'c' means that the database should be created if it doesnโ€™t already exist.
  5. # The result is a database object that can be used (for most operations) like a dictionary.
  6.  
  7. # If you create a new item, anydbm updates the database file:
  8. >>> db['felicity.png'] = 'Photo of Felicity Smoak.'
  9.  
  10. # When you access one of the items, anydbm reads the file:
  11. >>> print db['felicity.png']
  12. Photo of Felicity Smoak.
  13.  
  14. # If you make another assignment to an existing key, anydbm replaces the old value:
  15. >>> db['felicity.png'] = 'Photo of Felicity Smoak falling asleep on her laptop.'
  16. >>> print db['felicity.png']
  17. Photo of Felicity Smoak falling asleep on her laptop.
  18.  
  19. # Many dictionary methods, like keys and items, also work with database objects. So does iteration with a for statement.
  20. >>> for key in db:
  21.         print key
  22.  
  23. As with other files, you should close the database when you are done:
  24. >>> db.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement