I connect mysql by this code,The code works fine locally, but an error occurs after being deployed in streamlit cloud:
def create_connection():
"""Create a database connection and return the connection object."""
connection = None
logging.info(f"连接mysql数据库")
logging.info(f"host: {st.secrets.host}")
logging.info(f"user: {st.secrets.user}")
logging.info(f"password: {st.secrets.password}")
logging.info(f"database: {st.secrets.database}")
logging.info(f"port: {st.secrets.port}")
try:
connection = mysql.connector.connect(
host=st.secrets.host,
user=st.secrets.user,
password=st.secrets.password,
database=st.secrets.database,
port=st.secrets.port
)
if connection.is_connected():
print("Successfully connected to the database")
return connection
except Error as e:
print(f"The error '{e}' occurred")
return None
the error is:
raceback (most recent call last):
File “/mount/src/learnenglishword/models.py”, line 218, in get_all_words
with connection.cursor(buffered=True) as cursor:
AttributeError: ‘NoneType’ object has no attribute ‘cursor’
During handling of the above exception, another exception occurred
Traceback (most recent call last):
File “/home/adminuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py”, line 541, in _run_script
exec(code, module.dict)
File “/mount/src/learnenglishword/首页.py”, line 21, in
wordlist = models.get_all_words()
File “/mount/src/learnenglishword/models.py”, line 236, in get_all_words
connection.close()
AttributeError: ‘NoneType’ object has no attribute ‘close’