# Error fetching data from database to chatbot

**URL:** <https://forum.rasa.com/t/error-fetching-data-from-database-to-chatbot/27136>\
**Category:** Rasa Open Source\
**Created:** [April 7, 2020, 8:16am UTC](https://forum.rasa.com/t/error-fetching-data-from-database-to-chatbot/27136 "2020-04-07T08:16:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aman10](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/aman10/32/8440_2.png) [@Aman10](https://forum.rasa.com/u/Aman10)\
**Post date:** [April 7, 2020, 8:16am UTC](https://forum.rasa.com/t/error-fetching-data-from-database-to-chatbot/27136/1 "2020-04-07T08:16:28Z")

</div>

Here is my action.py file code

```
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

from typing import Any, Text, Dict, List
from rasa_core_sdk import Action
from rasa_sdk import Tracker
from rasa_sdk.executor import CollectingDispatcher
from rasa_core_sdk.events import SlotSet
import mysql.connector

class ActionSqlfetch(Action):

    def name(self):
        return "action_db"

    def run(self, dispatcher, tracker: Tracker, domain: Dict[Text, Any]):
        mydb = mysql.connector.connect(host="localhost",
                                       user="root",
                                       passwd="5000",
                                       database="world",
                                       auth_plugin='mysql_native_password'
                                       )
        mycursor = mydb.cursor()
        patient_id = int(tracker.get_slot('patientid'))
        sql = " Select * from genral_ward where patient_id = '%d' " % patient_id
        try:
            mycursor.execute(sql)
            if mycursor.execute(sql) == 0:
                print("Sorry could not find the relevent data, please check your id")
            result = mycursor.fetchone()
            for row in result:
                name = row[1]
                gender = row[2]
                disease = row[3]
                cost = row[7]
                patient = ("Patient Id =%d, Name = %s, Gender = %s, Disease =%s, Cost = %d" % (patient_id, name, gender, disease, cost))
                print(patient)
        except:
            dispatcher.utter_message("Error fetching data")
        finally:
            mydb.close()
        #response = " ""The details is follows"" ".format(result)
        dispatcher.utter_message("Here are the details, name:{}, disease:{}".format(name,disease))
        return [SlotSet('patientid',patient_id)]

```

and I’m getting this error

```
 dispatcher.utter_message("Here are the details, name:{}, disease:{}".format(name,disease))
UnboundLocalError: local variable 'name' referenced before assignment
127.0.0.1 - - [2020-04-07 13:40:47] "POST /webhook HTTP/1.1" 500 426 0.281024

```

Please help me, I need to do this project urgent

---

<div class="post-metadata">

**Author:** ![stavr](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/stavr/32/8352_2.png) [@stavr](https://forum.rasa.com/u/stavr)\
**Post date:** [April 7, 2020, 9:49am UTC](https://forum.rasa.com/t/error-fetching-data-from-database-to-chatbot/27136/2 "2020-04-07T09:49:36Z")

</div>

> [@Aman10](#):
>
> if mycursor.execute(sql) == 0: print(“Sorry could not find the relevent data, please check your id”)

Try changing print to `response = "Sorry could not find the relevent data, please check your id"`

Then, inside the `for row in result:` loop add the following line `response = "Here are the details, name:{}, disease:{}".format(name,disease)`

Finally, change

> [@Aman10](#):
>
> dispatcher.utter\_message(“Here are the details, name:{}, disease:{}”.format(name,disease))

to `dispatcher.utter_message(text=response)`

---

<div class="post-metadata">

**Author:** ![Aman10](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/aman10/32/8440_2.png) [@Aman10](https://forum.rasa.com/u/Aman10)\
**Post date:** [April 14, 2020, 7:03am UTC](https://forum.rasa.com/t/error-fetching-data-from-database-to-chatbot/27136/3 "2020-04-14T07:03:21Z")

</div>

Thanks @stavr that gave an idea, the error was in this part

> ```
> for row in result:
> name = row[1]
> gender = row[2]
> disease = row[3]
> cost = row[7]
> 
> ```

the result is actually a dictionary not a list, so i had access values using keys of dictionary

---

<div class="post-metadata">

**Author:** ![faiza\_conte](https://avatars.discourse-cdn.com/v4/letter/f/74df32/32.png) [@faiza\_conte](https://forum.rasa.com/u/faiza_conte)\
**Post date:** [October 4, 2020, 7:51am UTC](https://forum.rasa.com/t/error-fetching-data-from-database-to-chatbot/27136/4 "2020-10-04T07:51:53Z")

</div>

did you try to dispaly it to the webchat…i try to display it but the fetched data is like a sentence…i want it to be displayed in a list but faild…can you give me some explaination for that…thank you
