Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You may be able to use the <code>LOAD_FILE</code> function given in the answer to the question that Charles Sprayberry links to above. That would look like this:</p> <pre><code>mysql -p -u user -e "update my_table set body=load_file('/path/to/filename') where id=x;" dbname </code></pre> <p>If that doesn't work out for you (e.g., due to permissions issues or whatnot), then you solve your problem differently, by using features of Bash and <code>sed</code> to fix your existing command, as follows:</p> <p>Most special characters, such as newlines and whatnot, seem to be just fine inside single-quoted strings. The only characters that should be a problem are <code>'</code> (which would be interpreted as terminating the string) and <code>\</code> (which would be interpreted as introducing an escape sequence), and both of these can be addressed by prefixing a <code>\</code> (for example, <code>'\''</code> and <code>'\\'</code> are string literals denoting <code>'</code> and <code>\</code>, respectively). This <code>sed</code> script inserts a <code>\</code> before any <code>'</code> or <code>\</code>:</p> <pre><code>s/\('\|\\\)/\\\1/g </code></pre> <p>which you can invoke from Bash like this:</p> <pre><code>sed 's/\('"'"'\|\\\)/\\\1/g' filename </code></pre> <p>(for example, if <code>filename</code> contains this:</p> <pre><code>a \ b \ c d ' e ' f </code></pre> <p>then <code>sed 's/\('"'"'\|\\\)/\\\1/g' filename</code> will print this:</p> <pre><code>a \\ b \\ c d \' e \' f </code></pre> <p>). But you can't just wrap the above in <code>`...`</code>, because these characters are special to Bash as well, and it will get confused. (I'm assuming you're using Bash?) Instead, you need to use <code>$(...)</code>, which is a Bash notation that's equivalent to <code>`...`</code> except that it better handles various details like this. (I recommend always using <code>$(...)</code> over <code>`...`</code>.) So your overall command would be:</p> <pre><code>mysql -p -u user -e "update my_table set body='$(sed 's/\('"'"'\|\\\)/\\\1/g' filename)' where id=x;" dbname </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload