# SQL Results in a Dropdown

**URL:** <https://discuss.streamlit.io/t/sql-results-in-a-dropdown/4075>\
**Category:** Using Streamlit\
**Created:** [July 7, 2020, 11:56pm UTC](https://discuss.streamlit.io/t/sql-results-in-a-dropdown/4075 "2020-07-07T23:56:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![khushal111](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/khushal111/32/2283_2.png) [@khushal111](https://discuss.streamlit.io/u/khushal111)\
**Post date:** [July 7, 2020, 11:56pm UTC](https://discuss.streamlit.io/t/sql-results-in-a-dropdown/4075/1 "2020-07-07T23:56:26Z")

</div>

I wanted to make a dropdown that contain values from the response of a SQL query, it contains more than 35000 rows and its should searchable and selectable in a dropdown and based on that i want another dropdown to appear with a different query, i am very new to streamlit just wanted to know if this is possible, since this is a huge amount of data from SQL response

```
@st.cache
def get_advertisers():
    df_ad = pd.read_sql_query("""SELECT name FROM trc.publishers LIMIT ALL""",connection, index_col = None)
    return df_ad
advertiser_data = get_advertisers()
st.sidebar.title("Advertisers")
advertisers = st.sidebar.selectbox('Advertisers',[advertiser_data])

```

Above code shows data as below which is not iterable and formating is wrong

 ![Screen Shot 2020-07-08 at 06.46.30](https://global.discourse-cdn.com/streamlit/original/2X/b/b52dec633d6d878cafe6f314fff62631f8da3a90.png)

---

<div class="post-metadata">

**Author:** ![AhmedSalam22](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/ahmedsalam22/32/2129_2.png) [@AhmedSalam22](https://discuss.streamlit.io/u/AhmedSalam22)\
**Post date:** [July 8, 2020, 1:01am UTC](https://discuss.streamlit.io/t/sql-results-in-a-dropdown/4075/2 "2020-07-08T01:01:58Z")

</div>

try this

> <https://stackoverflow.com/questions/14835852/convert-sql-result-to-list-python>

---

<div class="post-metadata">

**Author:** ![randyzwitch](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/randyzwitch/32/58772_2.png) [@randyzwitch](https://discuss.streamlit.io/u/randyzwitch)\
**Post date:** [July 8, 2020, 12:53pm UTC](https://discuss.streamlit.io/t/sql-results-in-a-dropdown/4075/3 "2020-07-08T12:53:47Z")

</div>

Hi @khushal111, welcome to the Streamlit forums!

It looks you might be running into this issue:

> <https://github.com/streamlit/streamlit/issues/78>
>
> Low priority issue
> Problem
> st.selectbox throws an error when you pass it a dataframe even if that df only has one column.
> Solution
> It should...

The quick solution here is instead of passing in `advertiser_data` to the selectbox, pass in `advertiser_data["name"]`, so that the pandas Series is passed to the selectbox.

Separately, you might run into issues with having 35,000 rows in a drop-down. It’s not really a strong user experience either…is there a way you can do the top 1000 by occurrence, or have multiple drop-downs for people to hone in on which advertiser they are interested in?

Best,  
Randy

---

<div class="post-metadata">

**Author:** ![khushal111](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/khushal111/32/2283_2.png) [@khushal111](https://discuss.streamlit.io/u/khushal111)\
**Post date:** [July 10, 2020, 6:43am UTC](https://discuss.streamlit.io/t/sql-results-in-a-dropdown/4075/4 "2020-07-10T06:43:08Z")

</div>

Thanks @randyzwitch, advertiser[“name”] worked to pull advertiser\_data without index and also in the format of rows, and as you suggested i tried dividing the rows of advertiser into smaller chunks using below code, but it not working, the get\_advertisers() keeps on running without sending an data to streamlit, and in my case its not possible to have multiple dropdowns for people.

```
cur = connection.cursor()
n_rows = cur.execute("SELECT count(*) FROM trc.publishers").fetchone()[0]
print (n_rows)

@st.cache
def get_advertisers(query, connection, nrows, chunksize=100):
    start = 1
    dfs = []
    while start < nrows:
        df = pd.read_sql("%s LIMIT %s OFFSET %s" % (query, chunksize, start), connection)
        dfs.append(df)
    return pd.concat(dfs, ignore_index = True)

advertiser_data = get_advertisers("SELECT name from trc.publishers", connection, n_rows, 100)
st.sidebar.title("Advertisers")
advertisers = st.sidebar.selectbox('Select Advertisers',advertiser_data["name"])
```

---

<div class="post-metadata">

**Author:** ![randyzwitch](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/randyzwitch/32/58772_2.png) [@randyzwitch](https://discuss.streamlit.io/u/randyzwitch)\
**Post date:** [January 12, 2022, 9:40pm UTC](https://discuss.streamlit.io/t/sql-results-in-a-dropdown/4075/5 "2022-01-12T21:40:21Z")

</div>


