# MSSQL Database Connectivity

**URL:** <https://forum.rasa.com/t/mssql-database-connectivity/35422>\
**Category:** Rasa Certification Workshops\
**Created:** [October 3, 2020, 12:36pm UTC](https://forum.rasa.com/t/mssql-database-connectivity/35422 "2020-10-03T12:36:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![poojadeshmukh](https://avatars.discourse-cdn.com/v4/letter/p/58956e/32.png) [@poojadeshmukh](https://forum.rasa.com/u/poojadeshmukh)\
**Post date:** [October 3, 2020, 12:36pm UTC](https://forum.rasa.com/t/mssql-database-connectivity/35422/1 "2020-10-03T12:36:06Z")

</div>

Hi,

I am trying to establish database connectivity with MSSQL from Rasa. The requirement is to pass userid (entered by user) to a table in MSSQL and retrieve the records from table. Use these as response to user by Chatbot.

Thanks, Pooja

---

<div class="post-metadata">

**Author:** ![poojadeshmukh](https://avatars.discourse-cdn.com/v4/letter/p/58956e/32.png) [@poojadeshmukh](https://forum.rasa.com/u/poojadeshmukh)\
**Post date:** [October 5, 2020, 1:50pm UTC](https://forum.rasa.com/t/mssql-database-connectivity/35422/2 "2020-10-05T13:50:46Z")

</div>

Can someone please respond here?

Below are my files and I am getting error

```auto
(rasa-init-demo) C:\Users\DELL\RASASQL>rasa shell
2020-10-05 19:16:45 INFO root - Connecting to channel 'cmdline' which was specified by the '--connector' argument. Any other channels will be ignored. To connect to all given channels, omit the '--connector' argument.
2020-10-05 19:16:45 INFO root - Starting Rasa server on http://localhost:5005
2020-10-05 19:16:56 INFO root - Rasa server is up and running.
Bot loaded. Type a message and press enter (use '/stop' to exit):
Your input -> hi
Please enter your name.
Your input -> my name is Pooja
2020-10-05 19:17:33 ERROR rasa.core.actions.action - The model predicted the custom action 'action_get_name', but you didn't configure an endpoint to run this custom action. Please take a look at the docs and set an endpoint configuration via the --endpoints flag. https://rasa.com/docs/rasa/core/actions
2020-10-05 19:17:33 ERROR rasa.core.processor - Encountered an exception while running action 'action_get_name'. Bot will continue, but the actions events are lost. Please check the logs of your action server for more information.
2020-10-05 19:17:33 ERROR rasa.core.nlg.interpolator - Failed to fill utterance template 'Your name is {result}'. Tried to replace 'result' but could not find a value for it. There is no slot with this name nor did you pass the value explicitly when calling the template. Return template without filling the template.
Traceback (most recent call last):
  File "c:\users\dell\.conda\envs\rasa-init-demo\lib\site-packages\rasa\core\nlg\interpolator.py", line 28, in interpolate_text
    text = text.format(values)
KeyError: 'result'
Your name is {result}
Bye
Your input ->

```

NLU.md

## intent:greet

- hey

- hello

- hi

- good morning

- good evening

- hey there

## intent:goodbye

- bye

- goodbye

- see you around

- see you later

## intent:name\_entry

- my name is Pooja

- I am John

- This is Alex

Stories.md

## happy path

- greet

- name\_entry

domain.yml intents:

- greet

- goodbye

- name\_entry

entities:

- name

slots:

name:

```
type: unfeaturized

```

actions:

- utter\_greet

- utter\_show\_name

- utter\_goodbye

- action\_get\_name

responses:

utter\_greet:

- text: “Please enter your name.”

utter\_show\_name:

- text: “Your name is {result}”

utter\_goodbye:

- text: “Bye”

session\_config:

session\_expiration\_time: 60

carry\_over\_slots\_to\_new\_session: true

endpoints.yml #action\_endpoint:

url: “[http://localhost:5057/webhook](http://localhost:5057/webhook)”

actions.py

# This files contains your custom actions which can be used to run

# custom Python code.

# 

# See this guide on how to implement these action:

# [https://rasa.com/docs/rasa/core/actions/#custom-actions/](https://rasa.com/docs/rasa/core/actions/#custom-actions/)

# This is a simple example for a custom action which utters “Hello World!”

from typing import Any, Text, Dict, List

from rasa\_sdk import Action, Tracker

from rasa\_sdk.executor import CollectingDispatcher

import sqlite3, pyodbc

class ActionHelloWorld(Action):

```
 def name(self) -> Text:

     return "action_get_name"

 def submit(self,dispatcher,tracker,domain):

    try:

        conn = pyodbc.connect('Driver = {SQL Server};'

                        'Server=*;'

                        'Database=chatbot;'

                        'UID=*;'

                        'PWD=*;')

        name = tracker.get_slot('name')

        cursor = conn.cursor()

        msg = 'select fullname from chatbottest where name=?'

        curson.execute(msg,(name))

        for row in cursor:

            dispatcher.uttermessage([row.fullname+row.name])

    except:

        dispatcher.uttermessage("failed")

        return[]
```

---

<div class="post-metadata">

**Author:** ![stephens](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/stephens/32/1632_2.png) [@stephens](https://forum.rasa.com/u/stephens)\
**Post date:** [October 5, 2020, 8:37pm UTC](https://forum.rasa.com/t/mssql-database-connectivity/35422/3 "2020-10-05T20:37:59Z")

</div>

Hi Pooji,

Please post Rasa technical questions in the [Rasa OSS](http://forum.rasa.com/c/rasa/8) forum or [Rasa X](http://forum.rasa.com/c/rasa-x/21) forum (if they aren’t related to the topics covered by the workshop).

The error indicates that you don’t have an action server configured correctly in the [endpoints.yml](https://legacy-docs-v1.rasa.com/core/actions/#custom-actions).

Greg

---

<div class="post-metadata">

**Author:** ![Rajendra9](https://avatars.discourse-cdn.com/v4/letter/r/3ec8ea/32.png) [@Rajendra9](https://forum.rasa.com/u/Rajendra9)\
**Post date:** [December 31, 2020, 6:16am UTC](https://forum.rasa.com/t/mssql-database-connectivity/35422/4 "2020-12-31T06:16:10Z")

</div>

Hi, pooja you can connect database directly from custom actions you can visit w3schools there you can find there how to do with examples

def read(id\_person: int) -\> Person: db = pymysql.connect(SERVER, USER, PASSWORD, DB) cursor = db.cursor() sql = “SELECT \* FROM PERSON WHERE ID = ‘%d’” % (id\_person) try: cursor.execute(sql) results = cursor.fetchall() for row in results: first\_name = row[1] last\_name = row[2] age = row[3] sex = row[4] income = row[5] person = Person(id\_person, first\_name, last\_name, age, sex, income) except: print(“Error fetching data.”) finally: db.close() return person

If the ID is in a slot, you can easily pass it to the read function using the tracker.get\_slot function and then get the person object in return. Then using the dispatcher you could simply pass person.first\_name in the response.

---

<div class="post-metadata">

**Author:** ![poojadeshmukh](https://avatars.discourse-cdn.com/v4/letter/p/58956e/32.png) [@poojadeshmukh](https://forum.rasa.com/u/poojadeshmukh)\
**Post date:** [January 15, 2021, 12:56pm UTC](https://forum.rasa.com/t/mssql-database-connectivity/35422/5 "2021-01-15T12:56:29Z")

</div>

Thanks @Rajendra9 Will try this
