Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is best practice to create a small database in python?

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)

  1. Using postgreSQL, mySQL
  2. A nested dictionary something like this {'1.1.1.1':{next hop: {outgoing interface: source}}
  3. a dictionary with a list as a value {'1.1.1.1':[next hop, outgoing interface, source]}
  4. Any other option?

I feel that SQL would be the best solution but if not SQL, what is another option?

like image 872
monica Avatar asked Apr 27 '17 02:04

monica


People also ask

Which is best database for Python?

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.

Can you create a database using Python?

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.

What is the simplest database to use?

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.


1 Answers

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!

like image 188
Vosobe Kapsimanis Avatar answered Oct 20 '22 21:10

Vosobe Kapsimanis