CLI tool to help with running queries.
Pass through a sqlalchemy DB URL and a query to run a query and export to file. (You may need the relevant DB drivers installed also.)
python -m queryrunner show "sqlite:///" "SELECT 'Val' as col1"
python -m queryrunner to-csv "sqlite:///" "SELECT 'Val' as col1" "output.csv"
You can also use an environment variable to contain your connection string, and a file to contain your SQL query.
DB_URI="sqlite:///"
# example-query.sql
SELECT 'Val' as col1
python -m queryrunner to-csv "DB_URI" "example-query.sql" "output.csv"
You can pass query parameters as extra CLI options, but this only works for strings.
So this will work:
python -m queryrunner show "sqlite:///" "SELECT '5' as col1 WHERE col1=:val" --val 5
python -m queryrunner show "sqlite:///" "SELECT 5 as col1 WHERE CAST(col1 as text)=:val" --val 5
But this will not:
python -m queryrunner show "sqlite:///" "SELECT 5 as col1 WHERE col1=:val" --val 5