Hello everyone,
I am trying to connect an Oracle database to my rasa chatbot, I want to fetch data from that database which shall be used to answer the questions of a user. For example, if a user asks: “Who is the developer of Application Number 123?”. Then I want the chatbot to go into the application_info table (columns: application_id, developer_name) and answer the user with something like “The name of the developer of the Application Number 123 is John Taylor”. Can someone please walk me through the steps on how to go about with this?
Any ideas/suggestions/advice would be huge help!
Thanks!
Hello, you can use the MySQL connector module and this Python class to query your database:
import mysql.connector
class DatabaseConnection:
hostname = None
database = None
username = None
password = None
connection = None
cursor = None
query = None
def __init__(self, hostname, database, username, password):
if not self.connection:
self.hostname = hostname
self.database = database
self.username = username
self.password = password
self.connect()
def connect(self):
self.connection = mysql.connector.connect(
host = self.hostname,
user = self.username,
password = self.password,
database = self.database)
return self.connection
def disconnect(self):
self.cursor.close()
self.connection.close()
def query(self, query):
result = []
self.cursor = self.connection.cursor()
self.cursor.execute(query)
for row in self.cursor:
result.append(row)
return result
def count(self, table, condition = None):
return len(self.simple_query(table, '*', condition))
Then you can use it like so:
application_id = tracker.get_slot('application_id')
db = DatabaseConnection('localhost', 'my_database', 'druhmil', 'passw0rd')
results = db.query("SELECT developer_name FROM `application_info` "
f"WHERE application_id = '{application_id}'")
developer_name = results[0]
db.disconnect()