182 words
1 minute
Escaping a SQL query to use with curl and Datasette
Escaping a SQL query to use with curl and Datasette
I used this pattern to pass a SQL query to Datasette’s CSV export via curl and output the results, stripping off the first row (the header row) using tail -n +2
.
SQL queries need to be URL-encoded - I did that be echoing the SQL query and piping it to a Python one-liner that calls the urllib.parse.quote()
function.
curl -s "https://github-to-sqlite.dogsheep.net/github.csv?sql=$(echo 'select full_namefrom reposwhere rowid in ( select repos.rowid from repos, json_each(repos.topics) j where j.value = "datasette-io" ) and rowid in ( select repos.rowid from repos, json_each(repos.topics) j where j.value = "datasette-plugin" )order by updated_at desc' | python3 -c \ 'import sys; import urllib.parse; print(urllib.parse.quote(sys.stdin.read()))')" \ | tail -n +2
Here’s that SQL query in the Datasette web UI.
The output from the bash one-liner looks like this:
simonw/datasette-edit-schemasimonw/datasette-import-tablesimonw/datasette-dateutilsimonw/datasette-seabornsimonw/datasette-backupsimonw/datasette-yamlsimonw/datasette-schema-versionssimonw/datasette-graphqlsimonw/datasette-insertsimonw/datasette-copyablesimonw/datasette-auth-passwordssimonw/datasette-glitchsimonw/datasette-block-robotssimonw/datasette-saved-queriessimonw/datasette-psutilsimonw/datasette-auth-tokenssimonw/datasette-permissions-sqlsimonw/datasette-mediasimonw/datasette-atomsimonw/datasette-vegasimonw/datasette-jellyfishsimonw/datasette-leaflet-geojsonsimonw/datasette-template-sqlsimonw/datasette-render-markdownsimonw/datasette-auth-githubsimonw/datasette-mask-columnssimonw/datasette-jqsimonw/datasette-cluster-mapsimonw/datasette-upload-csvssimonw/datasette-publish-flysimonw/datasette-render-imagessimonw/datasette-render-timestampssimonw/datasette-configure-ftssimonw/datasette-search-allsimonw/datasette-render-htmlsimonw/datasette-show-errorssimonw/datasette-column-inspectsimonw/datasette-icssimonw/datasette-json-htmlsimonw/datasette-pretty-jsonsimonw/datasette-sqlite-fts4simonw/datasette-bplistsimonw/datasette-render-binarysimonw/datasette-ruresimonw/datasette-haversine
Escaping a SQL query to use with curl and Datasette
https://mranv.pages.dev/posts/escaping-a-sql-query-to-use-with-curl-and-datasette/