If I want to create a small database in Python, what would be the best practice to do it?
如果我想在Python中创建一个小型数据库,最好的做法是什么?
For instance, if I want to store output from Cisco's command "sh ip route" in a database,
例如,如果我想在数据库中存储思科命令“sh ip route”的输出,
O 1.1.1.1 [110/2] via 10.0.0.1, 00:00:23, FastEthernet0/0
stores these values:
存储这些值:
1.1.1.1 —> next hop, outgoing interface, source (like O, C, S)
1.1.1.1 - >下一跳,出接口,源(如O,C,S)
- Using postgreSQL, mySQL
- A nested dictionary something like this {'1.1.1.1':{next hop: {outgoing interface: source}}
- a dictionary with a list as a value {'1.1.1.1':[next hop, outgoing interface, source]}
- Any other option?
使用postgreSQL,mySQL
嵌套字典类似于这样的{'1.1.1.1':{next hop:{outgoing interface:source}}
列表作为值{'1.1.1.1':[下一跳,传出接口,源]}的字典
还有其他选择吗?
I feel that SQL would be the best solution but if not SQL, what is another option?
我觉得SQL会是最好的解决方案,但如果不是SQL,那么另一种选择是什么?
1 个解决方案
#1
4
routing tables look to have a form more similar to documents that relational tables in a RDBMS like Postgres or MySQL.
路由表看起来有一个更类似于像Postgres或MySQL这样的RDBMS中的关系表的文档的表单。
I would prefer to use a document oriented database in this case. Regarding which database to use it depends on your deployment scenario and application architecture.
在这种情况下,我更倾向于使用面向文档的数据库。关于使用哪个数据库取决于您的部署方案和应用程序体系结构。
If your aim is a database server which can be accessed via the network then Mongo is a very good choice. If you are going for a single-node application which will access the data only locally, then you can have a look in TinyDB.
如果您的目标是可以通过网络访问的数据库服务器,那么Mongo是一个非常好的选择。如果您要使用仅在本地访问数据的单节点应用程序,那么您可以查看TinyDB。
With TinyDB your code would look like:
使用TinyDB,您的代码将如下所示:
from tinydb import TinyDB, Query
db = TinyDB('/path/to/db.json')
db.insert({ 'Destination': '1.1.1.1',
'NextHop': '10.0.0.1',
'A': '*',
'P': 'B',
'Prf': 170',
....
})
And finally look for the individual routes as:
最后寻找各个路线:
route = Query()
db.search(route.Destination == '1.1.1.1')
# [{'Destination': '1.1.1.1','NextHop': '10.0.0.1','A': '*',...}]
or get all of them at once:
或者立刻获得所有这些:
db.all()
Hope it helps!
希望能帮助到你!
#1
4
routing tables look to have a form more similar to documents that relational tables in a RDBMS like Postgres or MySQL.
路由表看起来有一个更类似于像Postgres或MySQL这样的RDBMS中的关系表的文档的表单。
I would prefer to use a document oriented database in this case. Regarding which database to use it depends on your deployment scenario and application architecture.
在这种情况下,我更倾向于使用面向文档的数据库。关于使用哪个数据库取决于您的部署方案和应用程序体系结构。
If your aim is a database server which can be accessed via the network then Mongo is a very good choice. If you are going for a single-node application which will access the data only locally, then you can have a look in TinyDB.
如果您的目标是可以通过网络访问的数据库服务器,那么Mongo是一个非常好的选择。如果您要使用仅在本地访问数据的单节点应用程序,那么您可以查看TinyDB。
With TinyDB your code would look like:
使用TinyDB,您的代码将如下所示:
from tinydb import TinyDB, Query
db = TinyDB('/path/to/db.json')
db.insert({ 'Destination': '1.1.1.1',
'NextHop': '10.0.0.1',
'A': '*',
'P': 'B',
'Prf': 170',
....
})
And finally look for the individual routes as:
最后寻找各个路线:
route = Query()
db.search(route.Destination == '1.1.1.1')
# [{'Destination': '1.1.1.1','NextHop': '10.0.0.1','A': '*',...}]
or get all of them at once:
或者立刻获得所有这些:
db.all()
Hope it helps!
希望能帮助到你!