Build a Simple In-Memory Key-Value Database with Login and Admin Access (Python)




Have you ever wanted to build your own tiny database system using just Python — no JSON, no files, no external libraries?

In this post, I'll show you how to create a completely in-memory key-value database that includes:

User registration and login

Per-user private key-value storage

Admin access with tools to view all users and their data

No file or password hashing — just pure Python logic



---

What It Does

This program acts like a mini multi-user database system. Users can:

Register and log in with a username/password

Store their own key-value data

Retrieve their data using keys

List all their data


Admins can view all users and their data, but everything is temporary — once you quit, it's all gone.


---

Features

[x] In-memory only (no disk writes)

[x] Admin tools built-in

[x] Per-user separation of data

[x] Lightweight and beginner-friendly



---

Source Code

# In-memory key-value database with login and admin
users_db = {
    "admin": {
        "password": "admin123",
        "kv": {},
        "is_admin": True
    }
}

current_user = None

print("Welcome to the in-memory key-value database!")

while True:
    if not current_user:
        print("\n[L]ogin | [R]egister | [Q]uit")
        action = input("Choose: ").strip().upper()

        if action == "R":
            username = input("New username: ").strip()
            if username in users_db:
                print("User already exists.")
            else:
                password = input("New password: ").strip()
                users_db[username] = {
                    "password": password,
                    "kv": {},
                    "is_admin": False
                }
                print("User registered.")

        elif action == "L":
            username = input("Username: ").strip()
            password = input("Password: ").strip()
            if username in users_db and users_db[username]["password"] == password:
                current_user = username
                print(f"Logged in as {username}")
            else:
                print("Invalid login.")

        elif action == "Q":
            print("Goodbye.")
            break

        else:
            print("Invalid option.")

    else:
        user_data = users_db[current_user]
        print(f"\n[{current_user}] Options:")
        if user_data.get("is_admin"):
            print("[U]sers | [V]iew user data | [LO]gout | [Q]uit")
            action = input("Choose: ").strip().upper()

            if action == "U":
                print("All users:")
                for u in users_db:
                    print("-", u)

            elif action == "V":
                u = input("Enter username to view data: ").strip()
                if u in users_db:
                    print(f"{u}'s data: {users_db[u]['kv']}")
                else:
                    print("User not found.")

        else:
            print("[P]ut | [G]et | [L]ist | [LO]gout | [Q]uit")
            action = input("Choose: ").strip().upper()
            user_kv = user_data["kv"]

            if action == "P":
                k = input("Enter key: ")
                v = input("Enter value: ")
                user_kv[k] = v
                print("Saved.")

            elif action == "G":
                k = input("Enter key: ")
                print("Value:", user_kv.get(k, "Key not found."))

            elif action == "L":
                print("Your data:", user_kv)

        if action == "LO":
            print("Logged out.")
            current_user = None
        elif action == "Q":
            print("Goodbye.")
            break
        elif not user_data.get("is_admin") and action not in ["P", "G", "L", "LO", "Q"]:
            print("Invalid option.")


---

Usage Example

Welcome to the in-memory key-value database!

[L]ogin | [R]egister | [Q]uit
Choose: R
New username: john
New password: 1234
User registered.

[L]ogin | [R]egister | [Q]uit
Choose: L
Username: john
Password: 1234
Logged in as john

[john] Options:
[P]ut | [G]et | [L]ist | [LO]gout | [Q]uit
Choose: P
Enter key: color
Enter value: blue
Saved.

Choose: L
Your data: {'color': 'blue'}


---

Conclusion

This simple app is great for:

Learning how to manage users

Practicing data storage in memory

Building the foundation for a real database or app

মন্তব্যসমূহ

এই ব্লগটি থেকে জনপ্রিয় পোস্টগুলি

Build a Command-Line Tic Tac Toe Game in Python (With Download)