SQLTool
Description
This tool executes SQL queries and guides the agent in constructing correct queries based on the structures of all available tables in the database. It simplifies data retrieval, allowing users without advanced SQL knowledge to effectively query databases using natural language. The tool allows only SELECT queries to prevent any accidental changes to the database.
Requirements
To use SQLTool with different databases, ensure you have the following:
Sequelize: Version 6
Database Connector Package: The appropriate package for your database (e.g.,
ibm_db,mysql2,sqlite3, etc.)
Installation
Follow the table below to install the required packages for your database:
IBM Db2 for LUW
node-gyp, ibm_db
SQLite
sqlite3
MariaDB
mariadb (version 2)
MySQL
mysql2
PostgreSQL
pg
Microsoft SQL Server
tedious
Oracle
oracledb
To install Sequelize, run the following command:
Depending on the database you're using, install the required package(s). For example, if you're using IBM Db2 for LUW, run:
Replace node-gyp ibm_db with the appropriate package(s) for your database from the table above.
Usage
To use the SQLTool class, the following parameters must be supplied:
provider: The database provider. Supported values are:mysql,mariadb,postgres,mssql,db2,sqlite,oracle.
For SQLite
Other databases
Examples
Below are examples showcasing how to perform a search on the chinook SQLite database:
Question 1
Which country's customers spent the most?
On the first iteration, the agent tried to guess the SQL query based on the question, resulting in a missing table error. However, by utilizing the metadata returned with the error, it self-corrected on the second iteration and successfully generated the correct query.
Generated SQL
Answer
The country whose customers spent the most is the USA, with a total spending of $1040.49.
Question 2
Show the top 3 best selling artists in terms of revenue.
Generated SQL
Answer
The top 3 best selling artists in terms of revenue are: - Queen with a total revenue of $190.08 - Jimi Hendrix with a total revenue of $185.13 - Red Hot Chili Peppers with a total revenue of $128.77.
Sample Agent
A complete sample of an SQL agent is available here.