[ad_1]
i created 2 function in python
- connect to db and assign it dataframe
- get a unique list of selected column
but once i tried it crash and display the below error:
UnboundLocalError: local variable ‘df’ referenced before assignment
if df is not None:
def connect_db():
# try:
con = pyodbc.connect(
driver="ODBC Driver 17 for SQL Server",
Server = "127.0.0.1",# localhost
DATABASE="person",
UID="test",
PWD="test",
)
cursor = con.cursor()
df = pd.read_sql_query('''select m.ID,
m.name,
m.nickname,
m.mother_name,
m.birthdate
from [person].[dbo].[info] m
''',con)
return con
def get_name(con,df):
query_name = df.name.drop_duplicates()
return query_name
def main():
Connect_db()
if df is not None:
st.write(df)
list_name = get_name(con,df)
what i am doing wrong?
i appreciate help.
[ad_2]