Connect to a AWS RDS mySQL database example

Dear all,
does anyone have a code example of a Streamlit app connecting to a (AWS RDS) mySQL database and fetching data ?

Hi @blob123 -

Our documentation highlights an example of using a database connection with Streamlit:

You can connect to MySQL using the following:


Thanks Randy.
I was trying to use this code snippet Connect to a database - #9 by blob123 in the first place … but it didn’t work either.
Actually I have issues running queries on mySQL using a AWS RDS DB from Streamlit… if anyone has experienced it already.

I tried this way Connecting to your DB instance using IAM authentication and the AWS SDK for Python (Boto3) - Amazon Relational Database Service as well, but it failed to connect.

This is my code.

I get this error message now: TypeError: mySQL() takes no arguments

My class is not properly called … any suggestion ?

import streamlit as st
import mysql.connector
from mysql.connector import errorcode

config = {
'user': 'XXXX',
'host': '',
'raise_on_warnings': True

class mySQL:
    def init(self, st): = st
            self.cnx = mysql.connector.connect(**config)
            self.cursor = self.cnx.cursor()
        except mysql.connector.Error as err:
            if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
      "Something is wrong with your user name or password")
            elif err.errno == errorcode.ER_BAD_DB_ERROR:
      "Database does not exist")
      'Unknown error')
    def mysql_select(self, sql) :
        try :
            result = self.cursor.fetchall()
            if not result :
                return False, "Record not found"
            else :
                return True, result
        except mysql.connector.Error as err:
            return False, str(err.errno) + " : " + sql

query_to_execute = """SELECT * FROM TABLE"""
mysql = mySQL(st)
mysql. mysql_select(query_to_execute)