搁置:数据库选择

2024-04-25 17:51:57 发布

您现在位置:Python中文网/ 问答频道 /正文

shelvedocumentation表示:

The choice of which database package will be used (such as dbm, gdbm or bsddb) depends on which interface is available.

这是什么意思?如何确定选择哪个包?如何严格界定必须选择哪一个?什么样的数据库实现最适合使用?在


Tags: orofthepackagewhichasbewill
3条回答

在这里找到:
http://www.gossamer-threads.com/lists/python/python/13891

import shelve 
import gdbm 

def gdbm_shelve(filename, flag="c"): 
    return shelve.Shelf(gdbm.open(filename, flag)) 

db = gdbm_shelve("dbfile") 

ps
在链接页面有人也在某处发现了这个,但他的链接已经死了。在

How to determine which package choosen?

内置模块^{}可用于此目的。例如:

In [34]: db = anydbm.open('test.db', 'c')

In [35]: db['test'] = '123'

In [36]: db.close()

In [37]: import whichdb

In [38]: dir(whichdb)
Out[38]: 
['__builtins__',
 '__doc__',
 '__file__',
 '__name__',
 '__package__',
 '_dbmerror',
 'dbm',
 'os',
 'struct',
 'sys',
 'whichdb']

In [39]: whichdb.whichdb('test.db')
Out[39]: 'dbhash'

What database implementation best to use?

如果底层数据库引擎是dbm,则^{}模块会讨论一些限制(例如,名为dbm的Python模块,它与Unixndbm或BSD DB或ndbm的GNU-GDBM兼容性接口接口接口接口):

[...] this means that (the pickled representation of) the objects stored in the database should be fairly small, and in rare cases key collisions may cause the database to refuse updates.

目前还不清楚这是只适用于ndbm本身,还是兼容接口;“相当小”在数量上意味着什么;以及这些情况有多“罕见”。在

实际上,Ruby也有DBM的绑定,它有this to say

Original Berkeley DB was limited to 2GB of data. Dbm libraries also sometimes limit the total size of a key/value pair, and the total size of all the keys that hash to the same value. These limits can be as little as 512 bytes. That said, gdbm and recent versions of Berkeley DB do away with these limits.

我假设这没什么好担心的,因为不太可能使用ndbm,而且如果碰到这些限制,就会(希望)抛出一个描述性异常,此时我们需要进一步处理。在

我认为没有办法自己指定底层数据库。shelve使用anydbm,而anydbm则使用whichdb模块,该模块按以下顺序尝试以下底层实现

  • 数据库哈希
  • gdm公司
  • 数据库管理
  • 哑巴

您可以使用shelve.BsdDbShelfShelf的子类来强制使用bsd*d*b实现。在

相关问题 更多 >