write an app that has a sql query defined as a string and runs it in duckdb, then displays the results
To upload files, please first save the app
import streamlit as st
import duckdb
# Create a connection to DuckDB
conn = duckdb.connect(database=':memory:')
# Create some sample data
conn.execute("""
LOAD httpfs
""")
st.title('DuckDB Query Runner')
# Create a text area for SQL input
default_query = "SELECT * FROM users WHERE age > 25;"
query = st.text_area("Enter your SQL query:", value=default_query, height=100)
if st.button('Run Query'):
try:
# Execute the query and fetch results
results = conn.execute(query).fetchdf()
# Display results
st.subheader('Query Results:')
st.dataframe(results)
except Exception as e:
st.error(f'Error executing query: {str(e)}')
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?