Skip to main content

A lightweight, relational database built from scratch in Python

Project description

YesDB

A lightweight relational database built from scratch in Python with SQL support and B-tree storage.

Installation

pip install yesdb

Quick Start

Using the CLI

yesdb mydatabase.db
YesDB> CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER);
YesDB> INSERT INTO users VALUES (NULL, 'Alice', 30);
YesDB> INSERT INTO users VALUES (NULL, 'Bob', 25);
YesDB> SELECT * FROM users;

Using as a Library

from chidb import YesDB

# Create/open database
db = YesDB('myapp.db')

# Create table
db.execute('CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT, price REAL)')

# Insert data
db.execute("INSERT INTO products VALUES (NULL, 'Laptop', 999.99)")
db.execute("INSERT INTO products VALUES (NULL, 'Mouse', 29.99)")

# Query data
results = db.execute('SELECT * FROM products WHERE price < 100')
for row in results:
    print(row)

# Update & delete
db.execute("UPDATE products SET price = 899.99 WHERE name = 'Laptop'")
db.execute("DELETE FROM products WHERE price < 30")

# Close
db.close()

Context Manager

from chidb import YesDB

with YesDB('myapp.db') as db:
    db.execute('CREATE TABLE tasks (id INTEGER PRIMARY KEY, title TEXT)')
    db.execute("INSERT INTO tasks VALUES (NULL, 'Learn SQL')")
    results = db.execute('SELECT * FROM tasks')

Features

  • SQL Support: CREATE, SELECT, INSERT, UPDATE, DELETE, DROP, ALTER TABLE
  • Data Types: INTEGER, TEXT, REAL, BLOB
  • Query Features: WHERE, ORDER BY, LIMIT, OFFSET, DISTINCT
  • B-Tree Storage: Efficient indexing and data retrieval
  • No Dependencies: Pure Python implementation
  • Interactive Shell: Built-in SQL shell
  • Auto-increment: PRIMARY KEY auto-increment support

SQL Examples

-- Create table
CREATE TABLE users (id INTEGER PRIMARY KEY,name TEXT,email TEXT,age INTEGER)

-- Insert data
INSERT INTO users VALUES (NULL, 'Alice', 'alice@example.com', 30)
INSERT INTO users VALUES (NULL, 'Bob', 'bob@example.com', 25)

-- Query with conditions
SELECT * FROM users WHERE age > 25
SELECT name, email FROM users WHERE name = 'Alice'

-- Order and limit
SELECT * FROM users ORDER BY age DESC
SELECT * FROM users LIMIT 10 OFFSET 5

-- Update and delete
UPDATE users SET age = 31 WHERE name = 'Alice'
DELETE FROM users WHERE age < 18

-- Alter table
ALTER TABLE users ADD COLUMN country TEXT

-- Drop table
DROP TABLE users

Shell Commands

.help          Show help
.tables        List all tables
.schema        Show table schemas
.exit          Exit shell (.quit also works)

Setup Guide

1. Install YesDB

pip install yesdb

2. Verify Installation

# Check CLI works
yesdb --help

# Test Python import
python -c "from chidb import YesDB; print('YesDB installed successfully!')"

3. Create Your First Database

Option A: Using CLI

# Start the shell
yesdb my_database.db

# You'll see:
# YesDB version 0.1.0
# Enter ".help" for usage hints
# YesDB>

# Try some commands:
CREATE TABLE test (id INTEGER PRIMARY KEY, message TEXT);
INSERT INTO test VALUES (NULL, 'Hello World');
SELECT * FROM test;
.exit

Option B: Using Python

from chidb import YesDB

# Create database
db = YesDB('my_database.db')

# Create table
db.execute('CREATE TABLE users (id INTEGER PRIMARY KEY,username TEXT,created_at INTEGER)')

# Insert data
import time
db.execute(f"INSERT INTO users VALUES (NULL, 'admin', {int(time.time())})")

# Query
users = db.execute('SELECT * FROM users')
print(f"Found {len(users)} users")

db.close()

4. Working with Your Application

# app.py
from chidb import YesDB

class UserDatabase:
    def __init__(self, db_path='users.db'):
        self.db = YesDB(db_path)
        self._init_schema()

    def _init_schema(self):
        self.db.execute('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY,username TEXT,email TEXT)')

    def add_user(self, username, email):
        self.db.execute(f"INSERT INTO users VALUES (NULL, '{username}', '{email}')")

    def get_user(self, username):
        results = self.db.execute(f"SELECT * FROM users WHERE username = '{username}'")
        return results[0] if results else None

    def close(self):
        self.db.close()

# Usage
db = UserDatabase()
db.add_user('alice', 'alice@example.com')
user = db.get_user('alice')
db.close()

Security & Limitations

Security Features

  • Path validation (blocks system file access)
  • Resource limits (SQL length, record size)
  • Input validation (table/column names)

Important Limitations

  • Single-user only: Not designed for concurrent access
  • No encryption: Data stored in plaintext
  • No authentication: File access = database access
  • Local only: Not a client-server database

Recommended Use Cases

✅ Single-user desktop applications ✅ Development and prototyping ✅ Data analysis scripts ✅ Educational projects ✅ Embedded applications

❌ Multi-user web applications ❌ Concurrent access scenarios ❌ Sensitive data (without encryption) ❌ Production systems with high security requirements

Best Practices

from chidb import YesDB

# ✅ Good: Use in trusted environments
db = YesDB('local_data.db')

# ✅ Good: Validate user input
safe_input = user_input.replace("'", "''")
db.execute(f"INSERT INTO logs VALUES (NULL, '{safe_input}')")

# ❌ Avoid: Don't use with untrusted file paths
# ❌ Avoid: Don't store passwords in plaintext

Development

Install from Source

git clone https://github.com/AzharAhmed-bot/yes_db.git
cd yesdb
pip install -e .

Run Tests

pip install pytest
pytest

Requirements

  • Python 3.8+
  • No external dependencies

License

MIT License - see LICENSE file

Contributing

Contributions welcome! This is an educational project focused on learning database internals.

Links

Version

Current version: 0.1.1


Note: YesDB is an educational database. For production systems, consider using SQLite, PostgreSQL, or MySQL.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yesdb-0.1.2.tar.gz (59.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

yesdb-0.1.2-py3-none-any.whl (44.0 kB view details)

Uploaded Python 3

File details

Details for the file yesdb-0.1.2.tar.gz.

File metadata

  • Download URL: yesdb-0.1.2.tar.gz
  • Upload date:
  • Size: 59.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/1.0.0 requests/2.32.5 rfc3986/1.5.0 tqdm/4.67.1 urllib3/2.2.1 CPython/3.10.12

File hashes

Hashes for yesdb-0.1.2.tar.gz
Algorithm Hash digest
SHA256 7b31865e126d0eadd4d6fac49bd6819763a3d6c5af65d69f1966d8ed653e06a3
MD5 60ae22bf90b26a38b3fa92361619bc23
BLAKE2b-256 f48ba6123be9ee37d33c826604e95e6cbad3a8593a8efdf7015beacb838b588e

See more details on using hashes here.

File details

Details for the file yesdb-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: yesdb-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 44.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/1.0.0 requests/2.32.5 rfc3986/1.5.0 tqdm/4.67.1 urllib3/2.2.1 CPython/3.10.12

File hashes

Hashes for yesdb-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e3ff4c9b9252c9d4dc5f9b56893a6bf3b34ffc7c7503a7f35ec558ecfa16145c
MD5 3f64d9505564f9d7bc86e27234e23ccb
BLAKE2b-256 74054b33bfcfdec120132bceca1dc18f8752dd3ccdd4b22875fe93e80519ab7e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page