--- title: SQL Database slug: /sql-database --- import Icon from "@site/src/components/icon"; import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import PartialParams from '@site/docs/_partial-hidden-params.mdx'; The **SQL Database** component executes SQL queries on [SQLAlchemy-compatible databases](https://docs.sqlalchemy.org/en/20/). It supports any SQLAlchemy-compatible database, such as PostgreSQL, MySQL, and SQLite. For CQL queries, see the [**DataStax** bundle](/bundles-datastax). ## Query an SQL database with natural language prompts The following example demonstrates how to use the **SQL Database** component in a flow, and then modify the component to support natural language queries through an **Agent** component. This allows you to use the same **SQL Database** component for any query, rather than limiting it to a single manually entered query or requiring the user, application, or another component to provide valid SQL syntax as input. Users don't need to master SQL syntax because the **Agent** component translates the users' natural language prompts into SQL queries, passes the query to the **SQL Database** component, and then returns the results to the user. Additionally, input from applications and other components doesn't have to be extracted and transformed to exact SQL queries. Instead, you only need to provide enough context for the agent to understand that it should create and run a SQL query according to the incoming data. 1. Use your own sample database or create a test database.
Create a test SQL database 1. Create a database called `test.db`: ```shell sqlite3 test.db ``` 2. Add some values to the database: ```shell sqlite3 test.db " CREATE TABLE users ( id INTEGER PRIMARY KEY, name TEXT, email TEXT, age INTEGER ); INSERT INTO users (name, email, age) VALUES ('John Doe', 'john@example.com', 30), ('Jane Smith', 'jane@example.com', 25), ('Bob Johnson', 'bob@example.com', 35); " ``` 3. Verify that the database has been created and contains your data: ```shell sqlite3 test.db "SELECT * FROM users;" ``` The result should list the text data you entered in the previous step: ```shell 1|John Doe|john@example.com 2|Jane Smith|jane@example.com 3|John Doe|john@example.com 4|Jane Smith|jane@example.com ```
2. Add an **SQL Database** component to your flow. 3. In the **Database URL** field, add the connection string for your database, such as `sqlite:///test.db`. At this point, you can enter an SQL query in the **SQL Query** field or use the [port](/concepts-components#component-ports) to pass a query from another component, such as a **Chat Input** component. If you need more space, click