Last active
May 23, 2022 09:12
-
-
Save knbknb/e20a53aba0f7c9c13904740ad845d2b6 to your computer and use it in GitHub Desktop.
Bash: multiline SQL statements for mysql with the heredoc syntax and vertical output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| mysql -u root -psecret -Dworld 2>/dev/null -E -t<<EOF | |
| SELECT * | |
| FROM city | |
| WHERE NAME = 'amsterdam'; | |
| EOF | |
| # /* expected result -- in vertical format */: | |
| # *************************** 1. row *************************** | |
| # ID: 5 | |
| # Name: Amsterdam1 | |
| # CountryCode: NLD | |
| # District: Noord-Holland | |
| # Population: 731200 | |
| ## HOWEVER This will return nothing | |
| # - if credentials are wrong, or | |
| # - if these queries return an empty resultset | |
| # */ | |
| ################## Alternative ############################## | |
| # /* password set in an extra config file: | |
| # [client] | |
| # user="root" | |
| # password=secret | |
| # host=localhost | |
| # */ | |
| mysql --defaults-extra-file=$HOME/.mysql-credentials.cnf -Dworld -E -t<<EOF | |
| SELECT * | |
| FROM city | |
| WHERE NAME = 'amsterdam1'; | |
| EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment