Search code examples
pythonleveldbdatabasenosql

how can I iterate through all keys with python and leveldb


I want to use python's leveldb bindings to store data. I don't see in any of the docs how to iterate through all the keys without knowing them ahead of time. I see it is possible in the C bindings from this page: https://rawgit.com/google/leveldb/master/doc/index.html

leveldb::Iterator* it = db->NewIterator(leveldb::ReadOptions());
for (it->SeekToFirst(); it->Valid(); it->Next()) {
  cout << it->key().ToString() << ": "  << it->value().ToString() << endl;
}
assert(it->status().ok());  // Check for any errors found during the scan
delete it;

How can I do this from within python?


Solution

  • If you're using Plyvel, you can simply do for key, value in db: as per the docs.