Hi,
Is it possible to connect to a database and query from it and visualise the data from it.
Thanks & Best Regards
Michael
Hi,
Is it possible to connect to a database and query from it and visualise the data from it.
Thanks & Best Regards
Michael
Hi, Yes. It is possible. I was able to do it with SQL Server database.
Thanks a lot for your help.
Thanks & Best Regards
Michael
Is it possible to share a code snippet⌠How did you handle refresh when the database is updated with new data? I am very new to streamlit, and will appreciate a little help. Thanks
I created a class to handle my mySQL queries. It is just standard Python code for connecting to the database.
As for updates, if the database is being updated from an outside source, you will need to find a way to notify Streamlit of the change. I do my updates inside Streamlit - so I donât have that problem.
import streamlit as st
import mysql.connector
from mysql.connector import errorcode
config = {
âuserâ: âusernameâ,
âpasswordâ:âpasswordâ,
âhostâ: âlocalhostâ,
âdatabaseâ:âdatabaseâ,
âraise_on_warningsâ: True
}
class mySQL:
def init(self, st):
self.st = st
try:
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:
self.st.text("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
self.st.text("Database does not exist")
else:
self.st.text('Unknown error')
def mysql_select(self, sql) :
try :
self.cursor.execute(sql)
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
thank you so very much.
Hi,
Based on this class, how do I execute a query ?
Thanks.
you might also want to specify the database you want to use and whether it should be a local
one or hosted with a cloud provider(?).
In case of the latter, I highly recommend to check out the blogpost written by @AustinC. Streamlit â¤ď¸ Firestore
@blob123, I guess you create a new mySQL class object of and then call the mysql_select function:
query_to_execute = """select * from table"""
mysql = mySQL(st)
mysql. mysql_select(query_to_execute)
Best regards
Chris
Many thanks Chris.
I get this error message though:
AttributeError: âmySQLâ object has no attribute âconnectorâ
Not sure why exactly that happend. Did you do the imports?
I did.
I really donât manage to run this code.
Does anyone else have an experience connecting to mySQL, potentially with a RDS instance in the backend ?
The only thing I can think of is mysql-connector-python is not installed.
Thanks for your reply.
mysql-connector-python was well installedâŚ
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.