Simple In-Memory Database

Create a Python class Database that simulates a simple in-memory database. The class should have methods for creating (add), reading (get), updating (update), and deleting (remove) entries. Assume each entry in the database is a key-value pair.

Example 1:

Input: 
db = Database()
db.add("John", 25)
db.get("John")
Output: 25

Example 2:

Input: 
db = Database()
db.add("John", 25)
db.update("John", 30)
db.get("John")
Output: 30

You can use a Python dictionary to store the data in the memory.

class Database:
    def __init__(self):
        self.db = {}

    def add(self, key, value):
        self.db[key] = value

    def get(self, key):
        return self.db.get(key)

    def update(self, key, value):
        self.db[key] = value

    def remove(self, key):
        self.db.pop(key, None)

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!