3 ms·
If you're sweating quoting issues in here-documents, you can put the sqlite query in a separate file and tell sqlite to execute that file instead. Here's a hel
by rahimiali 5y ago
If you're sweating quoting issues in here-documents, you can put the sqlite query in a separate file and tell sqlite to execute that file instead.
Here's a helpful example from SO: https://stackoverflow.com/a/21759264/711585 https://stackoverflow.com/a/21759264/711585
- dashezup 5y agoTrue, I don't see the benefits of writing it in bash/shell script in this case. Plus you can get proper syntax highlighting for SQL in your text editor for ".sql" file. sqlite3 database.db <query.sql Some other DBMS have similar things, for MariadDB you could write it in a ".sql" file as well, setting variables and use it so that you can write a generic sql file. (seems you can't use the variable thing with sqlite3) MariaDB> SET @username='UserName@localhost'; MariaDB> SET @hostname='localhost'; MariaDB> SOURCE create-user.sql; Such things is usually cleaner/simpler than writing in bash.
- nerdponx 5y agoFor what it's worth, you can have "nested" syntax highlighting of SQL inside Bash scripts, with Tree Sitter in Neovim. You could also read the contents of the query file into a shell parameter with the `read` builtin.
- nerdponx 5y agoFun fact: you can do this with Sed and Awk, too. No more hideous unreadable one-liners.