RogueDB introduces a simplified database platform designed to reduce infrastructure work for startups and IT teams
Back to Tutorials
techTutorialbeginner

RogueDB introduces a simplified database platform designed to reduce infrastructure work for startups and IT teams

June 2, 20262 views5 min read

Learn to build a simplified database platform that reduces infrastructure work for startups, mimicking RogueDB's approach using Python and SQLite.

Introduction

In today's fast-paced startup environment, developers often find themselves spending valuable time on infrastructure setup rather than focusing on building core product features. This tutorial will guide you through creating a simplified database platform using Python and SQLite, mimicking the approach that RogueDB takes to reduce infrastructure work for startups. By the end of this tutorial, you'll have built a basic database management system that handles common database operations without requiring complex setup.

Prerequisites

To follow this tutorial, you'll need:

  • A computer with Python 3.6 or higher installed
  • A text editor or IDE (like VS Code, PyCharm, or even Notepad++)
  • Basic understanding of Python programming concepts
  • Some familiarity with databases and SQL concepts (though we'll explain the basics)

Step-by-Step Instructions

1. Setting up the Environment

First, we'll create a new Python project folder and set up our environment. Open your terminal or command prompt and run the following commands:

mkdir simple_db_project
 cd simple_db_project
 python -m venv db_env
 source db_env/bin/activate   # On Windows: db_env\Scripts\activate

This creates a new project directory and sets up a virtual environment to keep our project dependencies isolated.

2. Creating the Main Database Class

Now we'll create our main database class. Create a file called simple_db.py and add the following code:

import sqlite3
from contextlib import contextmanager

class SimpleDB:
    def __init__(self, db_name='app.db'):
        self.db_name = db_name
        self.init_db()

    def init_db(self):
        """Initialize the database and create tables if they don't exist"""
        with self.get_connection() as conn:
            conn.execute('''
                CREATE TABLE IF NOT EXISTS users (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    name TEXT NOT NULL,
                    email TEXT UNIQUE NOT NULL,
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                )
            ''')

    @contextmanager
    def get_connection(self):
        """Context manager for database connections"""
        conn = sqlite3.connect(self.db_name)
        try:
            yield conn
        finally:
            conn.close()

    def create_user(self, name, email):
        """Create a new user in the database"""
        with self.get_connection() as conn:
            cursor = conn.cursor()
            try:
                cursor.execute(
                    'INSERT INTO users (name, email) VALUES (?, ?)',
                    (name, email)
                )
                conn.commit()
                return cursor.lastrowid
            except sqlite3.IntegrityError:
                raise ValueError('Email already exists')

    def get_user(self, user_id):
        """Retrieve a user by ID"""
        with self.get_connection() as conn:
            cursor = conn.cursor()
            cursor.execute('SELECT * FROM users WHERE id = ?', (user_id,))
            return cursor.fetchone()

    def get_all_users(self):
        """Retrieve all users from the database"""
        with self.get_connection() as conn:
            cursor = conn.cursor()
            cursor.execute('SELECT * FROM users')
            return cursor.fetchall()

    def update_user(self, user_id, name=None, email=None):
        """Update a user's information"""
        with self.get_connection() as conn:
            cursor = conn.cursor()
            if name:
                cursor.execute('UPDATE users SET name = ? WHERE id = ?', (name, user_id))
            if email:
                try:
                    cursor.execute('UPDATE users SET email = ? WHERE id = ?', (email, user_id))
                except sqlite3.IntegrityError:
                    raise ValueError('Email already exists')
            conn.commit()

    def delete_user(self, user_id):
        """Delete a user from the database"""
        with self.get_connection() as conn:
            cursor = conn.cursor()
            cursor.execute('DELETE FROM users WHERE id = ?', (user_id,))
            conn.commit()

This class provides a simplified interface to a database. It handles connection management and basic CRUD (Create, Read, Update, Delete) operations. The context manager ensures connections are properly closed, and we've included error handling for duplicate emails.

3. Creating a Simple Test Script

Next, we'll create a test script to verify our database works correctly. Create a file called test_db.py:

from simple_db import SimpleDB

# Initialize our database
db = SimpleDB()

# Create some users
print("Creating users...")
user1_id = db.create_user('Alice Johnson', '[email protected]')
user2_id = db.create_user('Bob Smith', '[email protected]')

print(f"Created users with IDs: {user1_id}, {user2_id}")

# Retrieve all users
print("\nAll users:")
all_users = db.get_all_users()
for user in all_users:
    print(user)

# Retrieve a specific user
print("\nSpecific user:")
user = db.get_user(user1_id)
print(user)

# Update a user
print("\nUpdating user...")
try:
    db.update_user(user1_id, name='Alice Updated')
    print("User updated successfully")
except ValueError as e:
    print(f"Error updating user: {e}")

# Try to create a user with duplicate email
print("\nTrying to create duplicate user...")
try:
    db.create_user('Charlie Brown', '[email protected]')
except ValueError as e:
    print(f"Error: {e}")

# Delete a user
print("\nDeleting user...")
try:
    db.delete_user(user2_id)
    print("User deleted successfully")
except Exception as e:
    print(f"Error deleting user: {e}")

This script demonstrates all the database operations we've implemented. It shows how easy it is to create, read, update, and delete data without complex setup.

4. Running the Test

Now, run your test script to see the database in action:

python test_db.py

You should see output showing that users are created, retrieved, updated, and deleted successfully. Notice how the database handles errors like duplicate emails gracefully.

5. Adding More Features (Optional)

For a more advanced version, you can extend the database class with additional features:

def search_users(self, search_term):
    """Search for users by name or email"""
    with self.get_connection() as conn:
        cursor = conn.cursor()
        cursor.execute(
            'SELECT * FROM users WHERE name LIKE ? OR email LIKE ?',
            (f'%{search_term}%', f'%{search_term}%')
        )
        return cursor.fetchall()

def count_users(self):
    """Get the total number of users"""
    with self.get_connection() as conn:
        cursor = conn.cursor()
        cursor.execute('SELECT COUNT(*) FROM users')
        return cursor.fetchone()[0]

These additional methods show how easy it is to extend functionality while keeping the interface simple.

Summary

In this tutorial, we've built a simplified database platform that mimics the approach used by RogueDB to reduce infrastructure work for startups. We've created a Python class that handles database connections, performs basic CRUD operations, and includes proper error handling. The key benefits of this approach include:

  • Reduced Setup Time: No complex database configuration required
  • Easy to Use: Simple method calls for common operations
  • Robust Error Handling: Built-in protection against common issues
  • Scalable Design: Easy to extend with additional features

This simplified approach allows developers to focus on building product features rather than managing infrastructure, just like RogueDB aims to do for startups. While this example uses SQLite for simplicity, the same principles apply to more complex database systems, making it an excellent foundation for understanding how database abstraction layers work in modern development.

Source: TNW Neural

Related Articles