Generative AI with SQL database: A personal SQL Developer Chatbot πŸ˜ŠπŸ€–

Aqdas Ansari
2 min readAug 10, 2023

Writing tons of SQL queries to retrieve information from the database is obviously time consuming and take so much mental effort.

Now AI has the capability to increase the productivity level exponentially of SQL developers.

In this article, I will show you how you can build your own AI chatbot that takes the prompt (what you want). then it will write and execute SQL query and bring the results in front of you from the database.

I will try to make things as simple as possible.

Import Dependencies

from langchain.sql_database import SQLDatabase
from langchain_experimental.sql import SQLDatabaseChain
from langchain.llms import OpenAI
from langchain.agents import Tool
from sqlalchemy import create_engine, MetaData, Table, select

Connect with My SQL Database

If you have a database on live, that’s great. If not, you need to install XAMPP and start MYSQL.

Configure My SQL credentials and integrate it with python. create a connection string and pass it in create_engine function. It will establish a connection with database.

username = 'user_name'
password = 'your_password'
host = 'localhost'
port = 'port_name'
database_name = 'your_db'
connection_string = f"mysql+mysqlconnector://{username}:{password}@{host}:{port}/{database_name}”
engine =…

--

--