English 中文(简体)
字典储存数据库建议
原标题:Database recommendation for dictionary storage

Using Python I managed to make myself a kind of dictionary of terms and their meaning and it s rather big - x00,000 items (can t estimate right now as they are stored in multiple files by first letter).
Files are pickled dictionary objects with this structure:

dict{word, (attribute,
            kind,
            [meanings],
            [examples],
            [connections]
            )
    }

如果是硫磷酸 object,其关键是窒息和估价,那么该图则由扼杀或列出物体组成。

现在,我计划把他们放在轻松的三利基数据库中。 在我这样做之前,我想问一下,如果像我以前从未做过任何真正的数据库任务那样作出良好选择的话,那么三点建议。

我知道,答案取决于我想要做些什么(除数据结构外),但我要说的是,我只想把数据储存在一个地方(档案),并且是容易获得(频率)和可能改变的。

最佳回答

是的,我就这样一件事使用了qlite3。 字典价值必须首先通过:

import sqlite3
import pickle
import collections

class DBDict(collections.MutableMapping):
     Database driven dictlike object (with non-persistent in-memory option). 

    def __init__(self, db_filename= :memory: , **kwds):
        self.db = sqlite3.connect(db_filename)
        self.db.text_factory = str
        try:
            self.db.execute( CREATE TABLE dict (key text PRIMARY KEY, value text) )
            self.db.execute( CREATE INDEX key ON dict (key) )
            self.db.commit()
        except sqlite3.OperationalError:
            pass                # DB already exists
        self.update(kwds)

    def __setitem__(self, key, value):
        if key in self:
            del self[key]
        value = pickle.dumps(value)
        self.db.execute( INSERT INTO dict VALUES (?, ?) , (key, value))
        self.db.commit()

    def __getitem__(self, key):
        cursor = self.db.execute( SELECT value FROM dict WHERE key = (?) , (key,))
        result = cursor.fetchone()
        if result is None:
            raise KeyError(key)
        return pickle.loads(result[0])

    def __delitem__(self, key):
        if key not in self:
            raise KeyError(key)
        self.db.execute( DELETE FROM dict WHERE key = (?) , (key,))
        self.db.commit()

    def __iter__(self):
        return iter([row[0] for row in self.db.execute( SELECT key FROM dict )])

    def __repr__(self):
        list_of_str = [ %r: %r  % pair for pair in self.items()]
        return  {  +  ,  .join(list_of_str) +  } 

    def __len__(self):
        return len(list(iter(self)))



>>> d = DBDict(raymond= red , rachel= blue )
>>> d
{ rachel :  blue ,  raymond :  red }
>>> d[ critter ] = ( xyz , [1,2,3])
>>> d[ critter ]
( xyz , [1, 2, 3])
>>> len(d)
3
>>> list(d)
[ rachel ,  raymond ,  critter ]
>>> d.keys()
[ rachel ,  raymond ,  critter ]
>>> d.items()
[( rachel ,  blue ), ( raymond ,  red ), ( critter , ( xyz , [1, 2, 3]))]
>>> d.values()
[ blue ,  red , ( xyz , [1, 2, 3])]

以上将存放在单一档案中。 你们可以把物体当作普通字典。 由于这些价值观在单一领域被采纳,因此,qlite胜出给你任何额外的质疑选择。 其他公寓储存将受到类似的限制。 如果你需要写问题,反驳等级结构,考虑使用NSQL数据库。

问题回答

象文件储存数据库一样,向我分发。 http://couchdb.apache.org/“rel=“nofollow”http://couchdb.apache.org/





相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签