Python – 数据库管理器 (dbm) 包
在本文中,我们将了解 dbm,它是Python内置库中的一个包。 Python中的 dbm 包提供了一个类似字典的简单接口,格式为 DBM(数据库管理器),通常用于 Unix 操作系统。 dbm 以简单的键值对形式存储数据,就像字典一样,这使得从数据库中插入、编辑和检索数据变得更加容易。它通过使用单个主键(“键”)将数据存储在固定大小的块中。
dbm 包中包含三种类型的子模块:
- dbm.gnu: GNU 对 dbm 的重新解释
- dbm.ndbm:基于ndbm的接口
- dbm.dumb:可移植的 DBM 实现
以下是 dbm 包中可用的主要功能:
dbm.open()
该函数用于打开一个 dbm 数据库,如果不存在则创建一个新数据库。
Syntax: dbm.open(file, flag=’r’, mode=0o666)
Parameters: This function take following parameters:
- file: name of the file.
- flag: mode of permissions. which can be ‘r’, ‘w’, ‘c’, or ‘n’.
- ‘r’: open the existing database with permission to read only.
- ‘w’: open the existing database with permission to read and write.
- ‘c’: open the database for read and write, also create a new one if it doesn’t exists.
- ‘n’: Always create a new database with permission to both read and write.
- mode: The Unix mode of the argument which is a octal form default set to 0o666, used only when new database is to be created.
Return: The corresponding object address of the database file.
dbm.whichdb()
此函数尝试猜测几个可用的简单数据库模块(dbm.gnu、dbm.ndbm 或 dbm.dumb)中的哪一个应该用于打开给定文件。
Syntax: dbm.whichdb(filename)
Parameter: filename- Name of the file.
Returns: The function returns one of the following values :
- None: If the database doesn’t exists or it can’t be opened.
- (‘ ‘): An empty string, if the file exists but the file format can’t be guessed else
- The required module name: If the type is successfully detected then one of the string names is returned, ‘dbm.gnu’, ‘dbm.ndbm’ or ‘dbm.dumb’.
以下是 dbm 对象的内置方法:
- items(): This method returns the items contained in the database of caller object in form of key value pairs. (db is the caller object database).
- clear(): clear all the values present in the database.
- get(key): returns the value corresponding to key given in argument.
- keys(), iterkeys(): returns an iterable list containing keys of the dictionary.
- pop(key): Deletes / pops the key, value pair corresponding to key given in argument.
- setdefault(): set a default primary key given in the argument.
- sync(): Helps to synchronize data files and on disk directory.
- update(): updates the existing key value. Just like dictionary object.
- values(): iterate through all the values present in database.
- close(): Doesn’t take any argument nor returns anything. Just closes the caller object database. (db in this case)
以下是上述所有讨论方法/功能的实现:
代码:
Python3
# importing the dbm package
import dbm
# using the open function
# to create a new database named
# 'mydb' in 'n' mode, object
# returned in db variable.
db = dbm.open('mydb','n')
# inserting the new key and
# values in the database.
db['name'] = 'GeeksforGeeks'
db['phone'] = '8888'
db['Short name'] = 'GfG'
db['Date'] = '01/01/2000'
# getting and printing
# the value through get method.
print(db.get('name'))
print()
# printing the values of
# database through values()
# method (iterator).
for value in db.values():
print(value)
print()
# printing the values through
# key iterator.
for key in db.keys():
print(db.get(key))
print()
# poping out the key, value
# pair corresponding to
# 'phone' key.
db.pop('phone')
# printing the key, value
# pairs present in database.
for key, value in db.items():
print(key, value)
# clearing all the key values
# in database.
db.clear()
# Below loop will print nothing
# as database is cleared above.
for key, value in db.items():
print(key, value)
# closing the database.
db.close()
# This code is contributed by Amit Mangal.
输出 :
b'GeeksforGeeks'
b'GeeksforGeeks'
b'8888'
b'GfG'
b'01/01/2000'
b'GeeksforGeeks'
b'8888'
b'GfG'
b'01/01/2000'
b'name' b'GeeksforGeeks'
b'Short name' b'GfG'
b'Date' b'01/01/2000'