Convert natural language questions to SQL queries through a command-line interface.
nlsql-demo.mp4
- Clone the repository:
git clone https://github.com/TharaneshA/nlsql.git
cd nlsql
- Install dependencies:
pip install -r requirements.txt
- Install the package locally:
pip install -e .
To use NLSQL with your local database, you need to create a database connection profile. This allows the CLI to connect to your MySQL, PostgreSQL, or SQLite database and translate natural language into SQL queries.
Run the following command to start the profile creation wizard:
nlsql profile create <profile-name>
Replace <profile-name>
with something meaningful like my_local_db
.
You'll be prompted to enter your database connection information:
Prompt | Example (MySQL) | Example (SQLite) |
---|---|---|
Database type | MySQL | SQLite |
Host | localhost | (leave blank) |
Port | 3306 | (leave blank) |
Database name | nlsql_demo | /path/to/your/database.db |
Username | root | (leave blank) |
Password | your_password | (leave blank) |
Connection options | (optional) | (optional) |
π‘ Tip: For SQLite, you can use :memory:
to create an in-memory database.
After creating a profile, set it as the active one:
nlsql profile use <profile-name>
You can test the database connection by listing tables:
nlsql list tables
nlsql profile create my_mysql_db
# Prompts:
# Database type: MySQL
# Host: localhost
# Port: 3306
# Database name: nlsql_demo
# Username: root
# Password: your_password
nlsql profile use my_mysql_db
nlsql list tables
nlsql profile create my_sqlite_db
# Prompts:
# Database type: SQLite
# Host: (leave blank)
# Port: (leave blank)
# Database name: ./nlsql_demo.db
# Username: (leave blank)
# Password: (leave blank)
nlsql profile use my_sqlite_db
nlsql list tables
NLSQL supports multiple AI providers for natural language to SQL translation. You can configure your preferred AI service using the interactive setup process.
- Run the configuration command:
nlsql config setup
- Select your preferred AI provider from the available options:
- Gemini (Google)
- OpenAI (GPT-4)
- Anthropic (Claude)
- Grok (Coming soon)
- Enter your API key for the selected provider
To switch or update your AI provider configuration:
nlsql config set
This will launch an interactive menu where you can:
- Select a different AI provider
- Update API keys
- View current configuration
- π API keys and passwords are stored in plain text inside config files. For secure environments, consider using environment variables.
- π Profiles are stored in:
~/.nlsql/profiles/
- β
The active profile is stored in:
~/.nlsql/active_profile.txt
- π Environment variables for API keys:
- GEMINI_API_KEY
- OPENAI_API_KEY
- ANTHROPIC_API_KEY
- GROK_API_KEY
- List profiles:
nlsql profile list
- Create profile:
nlsql profile create <name>
- Switch profile:
nlsql profile use <name>
- Edit profile:
nlsql profile edit <name>
- Delete profile:
nlsql profile delete <name>
- Connect to database:
nlsql connect
- List tables:
nlsql list tables
- Show table schema:
nlsql describe <table-name>
- Cache database schema:
nlsql cache-schema
Basic query:
nlsql query "Show me all users from New York" -x
Query Options:
Option | Description |
---|---|
--edit , -e |
Edit SQL before execution |
--execute , -x |
Execute the query |
--save <name> |
Save query for later use |
--format <format> |
Output format (table /json /csv ) |
--export <file> |
Export results to file |
--explain |
Show query execution plan |
- List saved queries:
nlsql saved list
- Run saved query:
nlsql run <query-name>
- Delete saved query:
nlsql saved delete <query-name>
- View query history:
nlsql history
- List config:
nlsql config list
- Set config value:
nlsql config set KEY=VALUE
- Unset config value:
nlsql config unset KEY
- Create and use a database profile:
nlsql profile create mydb
nlsql profile use mydb
- Query with natural language:
nlsql query "Find all orders placed in the last 7 days" -x
- Save and reuse a query:
nlsql query "Show top 10 customers by order value" --save top_customers
nlsql run top_customers
- Export query results:
nlsql query "List all products with low stock" --format csv --export low_stock.csv
This project is licensed under the MIT License - see the LICENSE file for details.