【问题标题】:I am getting a SQL unknown sql server error in last lines of my code我的代码的最后几行出现 SQL 未知 sql server 错误
【发布时间】:2021-08-24 18:08:09
【问题描述】:
from logging import StreamHandler, exception
from sqlalchemy import create_engine
from re import search
from pandas._config.config import options
from pandas.core.frame import DataFrame
from pandas.core.tools import numeric
import streamlit as st       
import plotly.express as pt
import pandas as pd
import numpy as np
import os
import base64

#function
def filedownloader(database):
    csv = database.to_csv(index=False)
    b64 = base64.b64encode(csv.encode()).decode() 
    href = f'<a href="data:file/csv;base64,{b64}">Download csv file</a>'
    st.markdown(href,unsafe_allow_html=True)
    return href




#basic titles

st.title("Institution's Section")
st.sidebar.subheader("Settings")

# file uploader

fl=st.sidebar.file_uploader(label="Uplaod File in csv or xlsx format", type=['csv','xlsx'])
global df
if fl is not None:
    st.title("Performance Graph")
    try:
        df=pd.read_csv(fl)
    except Exception as e:
        print(e)
        df=pd.read_excel(fl)

#write table in web page

global columns
try:
    st.write(df)
    columns = list(df.columns)
except Exception as e:
    print(e)
    st.write("Please upload a file")

#Chart select

st.sidebar.subheader("Analysis section")
ch=st.sidebar.selectbox(
    label="Select the chart type",
    options=['Scatterplots','Lineplots','Histogram','Boxplot'],
    key="chart"
)

#Plot Settings

if ch =='Scatterplots':
    st.sidebar.subheader("Scatterplot Settings")
    
    try:
        x_values=st.sidebar.selectbox('X axis',options=columns)
        y_values=st.sidebar.selectbox('Y axis',options=columns)
        plot=pt.scatter(data_frame=df,x=x_values,y=y_values)
        st.plotly_chart(plot)
    except Exception as e:
        print(e)
elif ch=='Lineplots':
    st.sidebar.subheader("Lineplot Settings")
    
    try:
        x_values=st.sidebar.selectbox('X axis',options=columns)
        y_values=st.sidebar.selectbox('Y axis',options=columns)
        plot=pt.line(data_frame=df,x=x_values,y=y_values)
        st.plotly_chart(plot)
    except Exception as e:
        print(e)
elif ch=='Histogram':
    st.sidebar.subheader("Histogram Settings")
    
    try:
        x_values=st.sidebar.selectbox('X axis',options=columns)
        y_values=st.sidebar.selectbox('Y axis',options=columns)
        plot=pt.histogram(data_frame=df,x=x_values,y=y_values)
        st.plotly_chart(plot)
    except Exception as e:
        print(e)
elif ch=='Boxplot':
    st.sidebar.subheader("Boxplot Settings")
    
    try:
        x_values=st.sidebar.selectbox('X axis',options=columns)
        y_values=st.sidebar.selectbox('Y axis',options=columns)
        plot=pt.box(data_frame=df,x=x_values,y=y_values)
        st.plotly_chart(plot)
    except Exception as e:
        print(e)

#rank select


st.sidebar.subheader("Quick Search Rank")
radio=st.sidebar.radio(
    "What you want to see?",("Top five","Bottom five")
)

#rank settings

numeric_column = df.select_dtypes(include=np.number).columns.tolist()
if(radio=='Top five'):
    st.sidebar.subheader("Rank Settings")
    try:
        val=st.sidebar.selectbox('Select rank category',options=numeric_column)
        st.title("Quick Search Result")
        st.write(df.nlargest(5,val))
    except Exception as e:
        print(e)
elif(radio=='Bottom five'):
    st.sidebar.subheader("Rank Settings")
    try:
        val=st.sidebar.selectbox('Select rank category',options=numeric_column)
        st.title("Quick Search Result")
        st.write(df.nsmallest(5,val))
    except Exception as e:
        print(e)

#search

st.sidebar.subheader("Search")
val=st.sidebar.selectbox('Search desired column',options=columns)
st.sidebar.subheader("Search keyword")
user_input = st.sidebar.text_area("Type Keyword here", 0)
try: 
    dl = df[df[val] == type(df[val][1])(user_input)]
except Exception as e:
    pass

st.sidebar.subheader("Custom plot settings")
choose=st.sidebar.selectbox(
    label="Choose plot for selected data",
    options=['Scatterplot','Linearplot','Histogram','Boxplot',]
)
if(choose == 'Scatterplot'):
    try:
        st.sidebar.subheader("Scatterplot Settings")
        x_val=st.sidebar.selectbox('X axis',options=columns,key="scatter_x")
        y_val=st.sidebar.selectbox('Y axis',options=columns,key="scatter_y")
        plots=pt.scatter(data_frame=dl,x=x_val,y=y_val)
    except Exception as e:
        pass
elif (choose == 'Linearplot'):
    try:
        st.sidebar.subheader("Scatterplot Settings")
        x_val=st.sidebar.selectbox('X axis',options=columns,key="scatter_x")
        y_val=st.sidebar.selectbox('Y axis',options=columns,key="scatter_y")
        plots=pt.line(data_frame=dl,x=x_val,y=y_val)
    except Exception as e:
        pass
elif (choose == 'Histogram'):
    try:
        st.sidebar.subheader("Scatterplot Settings")
        x_val=st.sidebar.selectbox('X axis',options=columns,key="scatter_x")
        y_val=st.sidebar.selectbox('Y axis',options=columns,key="scatter_y")
        plots=pt.histogram(data_frame=dl,x=x_val,y=y_val)
    except Exception as e:
        pass
elif (choose == 'Boxplot'):
    try:
        st.sidebar.subheader("Scatterplot Settings")
        x_val=st.sidebar.selectbox('X axis',options=columns,key="scatter_x")
        y_val=st.sidebar.selectbox('Y axis',options=columns,key="scatter_y")
        plots=pt.box(data_frame=dl,x=x_val,y=y_val)
    except Exception as e:
        pass

#chart and search button

bt=st.sidebar.button("Search and Apply")
if(bt):
    st.title("Search Results")
    st.write(dl)
    st.title("Searh Analysis")
    st.plotly_chart(plots)

#Modify database
st.sidebar.subheader("Select Column to drop null values")
val1=st.sidebar.multiselect(
    label="Selet Columns",
    options=columns
)
bt_null_r=st.sidebar.button("Drop Null values")
if(bt_null_r):
    mdf=df.dropna(subset=val1, how='all')
    filedownloader(mdf)

# drop selected column
st.sidebar.subheader("Select Column to drop null values")
val2=st.sidebar.multiselect(
    label="Selet Columns",
    options=columns,key="column_multi"
)
bt_c=st.sidebar.button("Drop Columns")
if(bt_c):
    mdf=df.drop(val2,axis=1)
    filedownloader(mdf)
**#creating sql engine
engine=create_engine('mysql://root:Soumik18@@localhost:3306/college')
df.to_sql('Students',con=engine)**

我可能哪里出错了?我正在将 MySQL 与 Heidi SQL 一起使用。在最后两行中,我使用的是 SQL 代码,我的本地主机分配给 127.0.0.1,端口为 3306,密码是 Soumik18@。我能做什么?

我收到此错误:

OperationalError: (MySQLdb._exceptions.OperationalError) (2005, "Unknown MySQL server host '@localhost' (11003)") (此错误的背景:http://sqlalche.me/e/14/e3q8

【问题讨论】:

标签: python mysql mysql-connector-python streamlit


【解决方案1】:

需要检查的几件事:

  1. 您能否在脚本之外使用凭据连接到 MySQL 你提供了(mysql -u root -p)?如果是,那么您的连接脚本中可能存在配置问题。如果没有,请确认 MySQL 正在运行,然后重新检查您的密码。如果存在潜在的密码问题,可能值得通过在配置文件下添加 skip_grant_tables 并重新启动 MySQL 来重新启动 MySQL,这样您就可以在没有密码的情况下登录并重置 root 密码。
  2. 您是否以任何方式修改了 MySQL 配置,特别是 bind-address?默认情况下,MySQL 将其设置为 127.0.0.1,因此您可能想尝试使用 127.0.0.1 而不是 localhost 进行连接。
  3. 如果这是一台非 Windows 机器,是否为 localhost 正确配置了 /etc/hosts(例如:127.0.0.1 localhost)?

我使用不同的方法通过 Python 连接到 MySQL,所以我对 sqlalchemy 不太熟悉。

【讨论】:

    猜你喜欢
    • 2015-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 1970-01-01
    • 2013-07-13
    • 1970-01-01
    • 2021-07-24
    相关资源
    最近更新 更多