Any new calibre code should use the "new_api", so you should use the first method you described to get a db object.
Code:
from calibre.library import db;
db=db('Path to calibre library folder').new_api
Without the .new_api you get an instance of calibre.db.legacy.LibraryDatabase. Occasionally legacy methods are easier to use, but in my experience not very often.
A "new_api" object is an instance of calibre.db.cache.Cache. Look there for "public" methods, properties, and attributes. For example db.prefs is a method (line 609):
Code:
@read_api
def pref(self, name, default=None):
' Return the value for the specified preference or the value specified as ``default`` if the preference is not set. '
return self.backend.prefs.get(name, default)
Finally, and offered as a hint, in most cases you should specify a default when using .get(). In calibre, preferences can be and often are None. For example,
Code:
db.prefs.get('categories_using_hierarchy')
will return None or a list, meaning you must test it. If you instead use
Code:
db.prefs.get('categories_using_hierarchy', [])
then you will always get a list, simplifying subsequent processing.