Skip to content

Repository files navigation

QueryMind – AI SQL Assistant (CLI)

Convert Natural Language into SQL Queries from Your Terminal

Python • Google Gemini / OpenAI • MySQL • Oracle • SQLite • Rich CLI


Overview

QueryMind is an AI-powered command-line SQL assistant that allows users to interact with databases using natural language instead of writing SQL manually.

Simply ask questions like:

"Show all students whose CGPA is above 8."

QueryMind automatically:

  • Understands your request using an LLM
  • Discovers your database schema
  • Generates SQL for the correct database dialect
  • Validates the query for safety
  • Executes it on the connected database
  • Displays beautifully formatted results inside the terminal

It supports MySQL, Oracle, and SQLite, making it useful for learning SQL, database administration, analytics, and rapid querying.


Features

  • Natural Language → SQL conversion
  • Supports MySQL, Oracle, and SQLite
  • Automatic database schema discovery
  • AI understands tables, columns and relationships
  • Read-only SQL safety validation
  • Interactive terminal interface powered by Rich
  • SQL syntax highlighting
  • Query execution timing
  • AI-generated SQL explanations
  • Persistent query history
  • Built-in slash commands
  • Environment-based configuration
  • Easy database switching

Demo

==================================================
            QueryMind AI SQL Assistant
==================================================

Database : SQLite (college.db)
Status   : Connected

You > Show all students whose CGPA is above 8

Generated SQL

SELECT *
FROM students
WHERE cgpa > 8;

Executing...

┏━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━┓
┃ ID ┃ Name          ┃ Department ┃ CGPA ┃
┣━━━━╋━━━━━━━━━━━━━━━╋━━━━━━━━━━━━╋━━━━━━┫
┃ 1  ┃ Rahul Sharma  ┃ CSE        ┃ 8.4  ┃
┃ 2  ┃ Anjali Verma  ┃ ECE        ┃ 9.1  ┃
┃ 4  ┃ Priya Nair    ┃ CSE        ┃ 9.5  ┃
┗━━━━┻━━━━━━━━━━━━━━━┻━━━━━━━━━━━━┻━━━━━━┛

Rows Returned : 3
Execution Time : 4.2 ms

Key Features

Natural Language to SQL

Ask questions in plain English.

Examples:

  • Show all students
  • List employees earning more than 50000
  • Display customers from Kerala
  • Count total orders this month
  • Show departments ordered alphabetically

Multi-Database Support

QueryMind works seamlessly with:

  • SQLite
  • MySQL
  • Oracle Database

The AI automatically generates SQL based on the connected database.


Automatic Schema Discovery

Before generating SQL, QueryMind reads:

  • Tables
  • Columns
  • Data types
  • Primary Keys
  • Foreign Keys

This enables more accurate SQL generation.


SQL Safety Validation

Every generated query is validated before execution.

Blocked statements include:

  • DROP
  • DELETE
  • UPDATE
  • INSERT
  • ALTER
  • CREATE
  • TRUNCATE
  • EXEC
  • CALL

Only safe SELECT queries are executed.


Rich Terminal Interface

Built using the Rich library.

Features include:

  • Syntax highlighting
  • Beautiful tables
  • Status panels
  • Execution timers
  • Colourful banners
  • Interactive prompts

AI Query Explanation

Use:

/explain

The assistant explains the generated SQL in simple English.


Persistent Query History

Every session records:

  • Natural language prompt
  • Generated SQL
  • Execution time
  • Returned rows
  • Timestamp

History is stored locally inside:

history/history.json

Built-in Commands

Command Description
/help Show all available commands
/connect Connect to a database
/disconnect Disconnect current session
/schema Display discovered schema
/tables List all tables
/history View previous queries
/explain Explain the last SQL query
/clear Clear terminal
/exit Exit QueryMind

Project Structure

querymind/
│
├── main.py
├── config.py
├── database.py
├── schema.py
├── ai.py
├── validator.py
├── executor.py
├── history.py
├── prompts.py
├── utils.py
├── sample_db.py
├── requirements.txt
├── .env
│
├── history/
│   └── history.json
│
└── README.md

Installation

Clone the Repository

git clone https://github.com/yourusername/querymind.git

cd querymind

Create Virtual Environment

Windows

python -m venv venv

venv\Scripts\activate

Linux / macOS

python3 -m venv venv

source venv/bin/activate

Install Dependencies

pip install -r requirements.txt

Environment Variables

Create a .env file.

LLM_PROVIDER=gemini

GEMINI_API_KEY=YOUR_GEMINI_API_KEY

OPENAI_API_KEY=YOUR_OPENAI_API_KEY

DB_TYPE=sqlite

DB_HOST=localhost

DB_PORT=3306

DB_USER=root

DB_PASSWORD=

DB_NAME=college.db

Sample Database

Generate a demo SQLite database.

python sample_db.py

This creates:

college.db

containing:

  • students
  • departments
  • courses
  • enrollments

Running the Application

python main.py

Then simply ask:

Show all students

List employees from HR

Count all courses

Show students with CGPA above 8

Technologies Used

  • Python 3.12+
  • Google Gemini API
  • OpenAI API
  • Rich
  • SQLParse
  • SQLite
  • MySQL Connector
  • OracleDB
  • python-dotenv

Security

QueryMind is designed with database safety in mind.

  • Read-only query execution
  • SQL parsing and validation
  • Dangerous statements blocked
  • Encourages read-only database accounts
  • Prevents accidental destructive operations

Future Improvements

  • Export results to CSV, Excel and JSON
  • Query auto-completion
  • Database connection profiles
  • Voice-to-SQL
  • Docker support
  • Local LLM support (Ollama)
  • PostgreSQL support
  • Interactive charts
  • Web dashboard
  • Query optimisation suggestions

Contributing

Contributions are welcome.

  1. Fork the repository
  2. Create a new feature branch
git checkout -b feature/NewFeature
  1. Commit your changes
git commit -m "Add new feature"
  1. Push to your branch
git push origin feature/NewFeature
  1. Open a Pull Request

License

This project is licensed under the MIT License.


Author

Mahinsha E.A.

If you found this project helpful, consider giving it a ⭐ on GitHub.

About

AI-powered CLI assistant that converts natural language into safe SQL queries, executes them on MySQL, Oracle, and SQLite databases, and displays formatted results directly in the terminal.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages