If I want to create a small database in Python, what would be the best practice to do it?
For instance, if I want to store output from Cisco's command "sh ip route" in a database,
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)
I feel that SQL would be the best solution but if not SQL, what is another option?
SQLite. SQLite is probably the most straightforward database to connect to with a Python application since you don't need to install any external Python SQL modules to do so. By default, your Python installation contains a Python SQL library named sqlite3 that you can use to interact with an SQLite database.
Introduction. SQLAlchemy is a Python library for implementing SQL databases without using the SQL language itself. In other words, all you need to do is to implement your database using the Python language.
SQLite, a powerful Relational Database Management System (RDBMS), is also very easy to learn and to practice simple queries. It is very essential to become familiar with the basics of the most popular SQL Databases.
routing tables look to have a form more similar to documents that relational tables in a RDBMS like Postgres or MySQL.
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.
With TinyDB your code would look like:
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With