# Need help to connect sql database from phpmyadmin to my rasa chatbot in flask app

**URL:** https://forum.rasa.com/t/need-help-to-connect-sql-database-from-phpmyadmin-to-my-rasa-chatbot-in-flask-app/61606
**Category:** Rasa Open Source
**Created:** [April 11, 2024, 5:23pm UTC](https://forum.rasa.com/t/need-help-to-connect-sql-database-from-phpmyadmin-to-my-rasa-chatbot-in-flask-app/61606 "2024-04-11T17:23:00Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![andrea924breaux](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@andrea924breaux](https://forum.rasa.com/u/andrea924breaux)
#### Post date: [April 15, 2024, 5:20am UTC](https://forum.rasa.com/t/need-help-to-connect-sql-database-from-phpmyadmin-to-my-rasa-chatbot-in-flask-app/61606/2 "2024-04-15T05:20:19Z")

</div>

To connect your Rasa chatbot Flask app to your MySQL database and fetch the responses based on the user’s intents, you can follow these steps:

- **Install the required dependencies:**

1. Flask: pip install flask
2. PyMySQL (for interacting with the MySQL database): pip install pymysql

- **Update your app.py file:**

```auto
from flask import Flask, request, jsonify
import pymysql

app = Flask( __name__ )

# MySQL connection details
db_host = 'localhost'
db_user = 'your_username'
db_password = 'your_password'
db_name = 'backup'

# Connect to the MySQL database
connection = pymysql.connect(
    host=db_host,
    user=db_user,
    password=db_password,
    db=db_name
)

@app.route('/chat', methods=['POST'])
def chat():
    # Get the user's message from the request
    message = request.json['message']

    # Query the database to fetch the response
    with connection.cursor() as cursor:
        sql = "SELECT nom FROM atsufas1 WHERE message = %s"
        cursor.execute(sql, (message,))
        result = cursor.fetchone()
        if result:
            response = result[0]
        else:
            response = "I'm sorry, I don't have a response for that."

    return jsonify({'response': response})

if __name__ == ' __main__':
    app.run()

```

- **Update your nlu.yml file** :

```auto
version: "3.1"

nlu:
- intent: greet
  examples: |
    - hey
    - hello
    - hi
    - good morning
    - good evening
    - hey there

- intent: goodbye
  examples: |
    - bye
    - goodbye
    - see you later
    - catch you later

- intent: ask_name
  examples: |
    - what is your name
    - who are you
    - what should i call you

# Add more intents here, for example:
# - intent: ask_weather
# examples: |
# - what's the weather like
# - how's the weather today
# - is it raining outside

```

- **Update your Rasa model:**

After making changes to your nlu.yml file, you’ll need to update your Rasa model. You can do this by running the following command in your terminal:

```auto
rasa train

```

_Now, when a user sends a message to your Rasa chatbot, the Flask app will query the MySQL database to fetch the corresponding response based on the user’s intent (defined in the nlu.yml file). The response is then returned to the user._

_Note that you’ll need to replace the [KrogerFeedback](https://www-ikrogerfeedback.com) MySQL connection details (db\_host, db\_user, db\_password, db\_name) with your own values. Also, make sure the atsufas1 table in your backup database has a column named nom that contains the response text._

_If you have any further questions or issues, feel free to ask!_

---

_[View the full topic](https://forum.rasa.com/t/need-help-to-connect-sql-database-from-phpmyadmin-to-my-rasa-chatbot-in-flask-app/61606)._
