Skip to main content

AI-powered SQL query generator using OpenAI GPT or Anthropic Claude. Automatic schema extraction, query validation, and optimization for MySQL and PostgreSQL.

Project description

SQL Buddy 🤖

PyPI version Python Support License: Apache 2.0

AI-powered SQL query generator by MISTER IKS

Generate accurate SQL queries from natural language using OpenAI GPT or Anthropic Claude. Supports MySQL & PostgreSQL with automatic schema analysis, query validation, and optimization.

✨ Key Features

  • Multi-Database: MySQL & PostgreSQL support
  • Dual AI Support: OpenAI GPT-4 & Anthropic Claude
  • Built-in Safety: SQL injection detection & query validation
  • Query Tools: Generation, explanation, optimization, variations
  • CLI & Library: Use as Python package or command-line tool
  • Safe by Default: Validates queries before execution

📦 Installation

pip install pcybox-sqlbuddy

Requirements

  • Python 3.8+
  • MySQL or PostgreSQL database
  • OpenAI API key or Anthropic API key

🚀 Quick Start

Python Library

from sqlbuddy import SQLBuddy

# Using context manager (recommended)
with SQLBuddy(
    db_type="mysql",
    host="localhost",
    user="root",
    password="your_password",
    database="your_db",
    llm_provider="openai",  # or "claude"
    api_key="your_api_key"
) as buddy:
    
    # Generate query from natural language
    result = buddy.generate_query("Show users registered in last 30 days")
    print(result["query"])
    # Output: SELECT * FROM users WHERE created_at >= DATE_SUB(NOW(), INTERVAL 30 DAY)
    
    # Generate and execute in one call
    data = buddy.generate_and_execute("Top 10 customers by order value")
    print(data["execution"]["data"])

CLI Usage

# Test database connection
sqlbuddy test --host localhost --user root --database mydb

# Generate query
sqlbuddy generate "Show all active users" \
  --host localhost --database mydb

# Generate and execute
sqlbuddy generate "Count orders by status" --execute \
  --host localhost --database mydb

# Show database schema
sqlbuddy schema --host localhost --database mydb

# Explain query
sqlbuddy explain "SELECT * FROM users WHERE created_at > NOW() - INTERVAL 30 DAY"

# Optimize query
sqlbuddy optimize "SELECT * FROM users WHERE email LIKE '%@gmail.com'"

🔧 Configuration

Using Environment Variables

Create a .env file in your project:

# Database Configuration
SQLBUDDY_DB_TYPE=mysql
SQLBUDDY_DB_HOST=localhost
SQLBUDDY_DB_PORT=3306
SQLBUDDY_DB_USER=root
SQLBUDDY_DB_PASSWORD=your_password
SQLBUDDY_DB_NAME=your_database

# LLM Configuration
OPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=sk-ant-...

Then use it:

from sqlbuddy import SQLBuddy
from dotenv import load_dotenv

load_dotenv()

# Environment variables are automatically loaded
buddy = SQLBuddy(llm_provider="openai")

📖 Usage Examples

1. Generate Multiple Query Variations

with SQLBuddy(database="sales_db") as buddy:
    variations = buddy.generate_multiple_queries(
        "Show sales performance by region",
        num_variations=3
    )
    
    for i, var in enumerate(variations, 1):
        print(f"\nApproach {i}:")
        print(var["query"])

2. Query Optimization

with SQLBuddy(database="analytics") as buddy:
    slow_query = "SELECT * FROM orders WHERE YEAR(created_at) = 2024"
    
    optimization = buddy.optimize_query(slow_query)
    
    print("Original:", optimization["original_query"])
    print("Optimized:", optimization["optimized_query"])
    print("\nDetails:", optimization["optimization_details"])

3. Query Explanation

with SQLBuddy(database="app_db") as buddy:
    query = """
    SELECT c.name, COUNT(o.id) as order_count
    FROM customers c
    LEFT JOIN orders o ON c.id = o.customer_id
    GROUP BY c.id
    HAVING order_count > 5
    """
    
    explanation = buddy.explain_query(query)
    print(explanation["analysis"])

4. Schema Exploration

with SQLBuddy(database="ecommerce") as buddy:
    # Get schema summary
    summary = buddy.get_schema_summary()
    print(f"Total Tables: {summary['total_tables']}")
    print(f"Total Relationships: {summary['total_relationships']}")
    
    # Get specific table info
    table_info = buddy.get_table_info("users")
    for col in table_info['columns']:
        print(f"  {col['name']}: {col['type']}")

5. Safe Query Execution

with SQLBuddy(database="app") as buddy:
    # Generate query
    result = buddy.generate_query("Delete inactive users")
    
    # Check if destructive
    if result["validation"]["is_destructive"]:
        print("⚠️ This is a destructive operation!")
        confirm = input("Continue? (yes/no): ")
        
        if confirm.lower() == "yes":
            exec_result = buddy.execute_query(
                result["query"],
                allow_destructive=True
            )
            print(f"Affected rows: {exec_result['row_count']}")

🛡️ Security Features

SQL Buddy includes multiple layers of security:

Automatic Validation

# All queries are validated by default
result = buddy.generate_query("Your description")

# Check validation results
if not result["validation"]["is_valid"]:
    print("Errors:", result["validation"]["errors"])
if result["validation"]["warnings"]:
    print("Warnings:", result["validation"]["warnings"])

Protected Operations

  • SQL injection pattern detection
  • Destructive operation blocking (DROP, TRUNCATE, DELETE without WHERE)
  • Unbalanced parentheses/quotes detection
  • Suspicious pattern alerts

Safe Execution

# Safe by default
buddy.execute_query(query)  # Validated automatically

# Destructive operations need explicit permission
buddy.execute_query(
    "DELETE FROM old_logs",
    allow_destructive=True  # Required for destructive ops
)

🖥️ CLI Reference

Commands

Command Description Example
generate Generate SQL from natural language sqlbuddy generate "Show active users"
execute Execute SQL query sqlbuddy execute "SELECT * FROM users"
schema Show database schema sqlbuddy schema --summary
explain Explain SQL query sqlbuddy explain "SELECT ..."
optimize Optimize SQL query sqlbuddy optimize "SELECT ..."
test Test database connection sqlbuddy test --host localhost

Global Options

--db-type [mysql|postgresql]   Database type (default: mysql)
--host HOST                    Database host
--port PORT                    Database port
--user USER                    Database user
--password PASSWORD            Database password
--database DATABASE            Database name
--llm-provider [openai|claude] LLM provider
--verbose, -v                  Verbose output

Use sqlbuddy COMMAND --help for command-specific options.

🤝 Contributing

Contributions are welcome! Please see CONTRIBUTING.md for guidelines.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📄 License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Copyright 2025 Ibrahima Khalilou lahi SAMB

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

💼 Commercial Support

Need help integrating SQL Buddy into your project? Looking for custom features or enterprise support?

I offer:

  • 🎯 Custom development and integration
  • 🔒 Security audits and consulting
  • 📚 Training and workshops
  • ⚡ Priority support
  • 🏢 Enterprise licensing

Contact us: shadow@pcybox.com

📞 Support & Community

🙏 Acknowledgments

  • Built with ❤️ by pcybox
  • Inspired by the need for faster SQL development

⭐ Star History

If you find SQL Buddy helpful, please consider giving it a star on GitHub!

Star History Chart


Made with ❤️ by IKS - Backend Development & Cybersecurity Solutions

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

pcybox_sqlbuddy-0.1.0.tar.gz (28.8 kB view details)

Uploaded Source

Built Distribution

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

pcybox_sqlbuddy-0.1.0-py3-none-any.whl (31.3 kB view details)

Uploaded Python 3

File details

Details for the file pcybox_sqlbuddy-0.1.0.tar.gz.

File metadata

  • Download URL: pcybox_sqlbuddy-0.1.0.tar.gz
  • Upload date:
  • Size: 28.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for pcybox_sqlbuddy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 420ee33d19377575ee2be0f6782d6c50e6dc5b5f162b6b89426176346f4d8d20
MD5 d8e0e9f6012dc1a6928243d30bfac550
BLAKE2b-256 eef885ae3f378c60a88bf92f375ce10bc4b174cbe302211f415440e9c0aeac0e

See more details on using hashes here.

File details

Details for the file pcybox_sqlbuddy-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pcybox_sqlbuddy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c328337021b409b0b66fde9bc40f3719d6c5a1b9a760050d8090be950c1444c4
MD5 813a2d40284b55b8fe56bda0d6dcb079
BLAKE2b-256 2251306a60aae83767da5e43a0e5a21af0efea25a29619e719e129ebe21372f8

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