In linux, you can use the command
pip install lmdb
Install the lmdb package.
----
- lmdb database file generation
- Add, change, delete
- surname Zha
1. Generate an empty lmdb database file
# -*- coding: utf-8 -*- import lmdb # If there is no or file in the train folder, an empty one will be generated, if there is, it will not be overwritten # map_size defines the maximum storage capacity in kilobytes (kb), the following defines 1TB capacity env = ("./train",map_size=1099511627776) ()
2, LMDB data add, modify, delete
# -*- coding: utf-8 -*- import lmdb # map_size defines the maximum storage capacity in kilobytes (kb), the following defines 1TB capacity env = ("./train", map_size=1099511627776) txn = (write=True) # Add data and keys (key = '1', value = 'aaa') (key = '2', value = 'bbb') (key = '3', value = 'ccc') # Delete data by key value (key = '1') # Modify data (key = '3', value = 'ddd') # Commit changes via the commit() function () ()
3. Querying the LMDB database
# -*- coding: utf-8 -*- import lmdb env = ("./train") # The parameter write is set to True to write. txn = (write=True) ############################################ add, modify, delete data # Add data and keys (key = '1', value = 'aaa') (key = '2', value = 'bbb') (key = '3', value = 'ccc') # Delete data by key value (key = '1') # Modify data (key = '3', value = 'ddd') # Commit changes via the commit() function () ############################################ query lmdb data txn = () # get function queries data by key value print (str(2)) # Iterate over all data and keys with cursor() for key, value in (): print (key, value) ############################################ ()
4. Read the contents of the existing .mdb file
# -*- coding: utf-8 -*- import lmdb env_db = ('trainC') # env_db = ("./trainC") txn = env_db.begin() # get function queries the data by key value, if the key value to be queried does not have corresponding data, then output None print (str(200)) for key, value in (): # traversing print (key, value) env_db.close()
Above is the detailed content of Python LMDB library usage examples, more information about Python LMDB library please pay attention to my other related articles!