Error fetching data from database to chatbot

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

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

to dispatcher.utter_message(text=response)

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

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