Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create a /usr/bin/mysql wrapper program via IO redirection?
    primarykey
    data
    text
    <p>Presently, I'm thoroughly dissatisfied by the command-line program /usr/bin/mysql! It does not, e.g., let me leverage:</p> <p>(a) various Unix tools (like, grep, sed...), and</p> <p>(b) Unix concepts such as io-redirection, piping.</p> <p><em>(Btw, IIRC, a command-line shell for Sybase used to allow piping and sufficiently powerful, Unix-style command-line usage.)</em></p> <p>So... I'm wondering, if it's possible to create a shell wrapper on my own for this sucker?</p> <p>My first version looks like this.</p> <pre><code># #!/bin/bash sqlUser=USER_NAME sqlPassword= # The password will be taken from ~/.my.cnf . # Sql statements specified via stdin. while read -p 'SQL&gt; ' x do if [ "$x" = "quit" ]; then # Quit the loop. echo "Goodbye." break else eval "mysql -u$sqlUser -e '$x' " echo fi done </code></pre> <p>Sample usage:</p> <pre><code>[sd@host:~/tmp] $ ./mysqlsh SQL&gt; select * from bankdb.loan; +---------+---------------+--------+ | loan_no | branch_name | amount | +---------+---------------+--------+ | L-11 | Round Hill | 900 | | L-14 | Downtown | 1500 | | L-15 | Perryridge | 1500 | | L-16 | Perryridge | 1300 | | L-17 | Downtown | 1000 | | L-23 | Redwood | 2000 | | L-93 | Mianus | 500 | +---------+---------------+--------+ SQL&gt; quit Goodbye. [sd@host:~/tmp] $ </code></pre> <ol> <li><p>Now, while I could add the piping/redirection feature by saying to myself that... anything preceding the semi-colon ';' in the user input to read() would be the SQL, and anything following the ';' would be the Unix shell constructs to be passed straight to eval() above. <strong>But, I'm not sure if this is the greatest way</strong>!</p></li> <li><p>Another very serious limitation that would still persist is: There will be no concept of a session... due to a new invocation of /usr/bin/mysql in every iteration of the while-loop. <strong>So, what I'd really like to be able to do</strong> is: <strong>(a)</strong> launch one, single instance of /usr/bin/mysql, and <strong>(b)</strong> <em>somehow</em> wire /usr/bin/mysql's stdin, stdout, stderr streams to my wrapper program's. <strong>Now, is this stunt possible?</strong> Note that I tried [sd@host:~/tmp] $ mysql -uSD 2>&amp;1 | tee sql.out and it did not work as 'expected'. Meaning, I did not get the familiar mysql interactive messages to my sql.out file, so wondering what's going on here!!</p></li> <li><p>If this is not possible via shell (bash), could this be done via a C-program...? that uses MySQL's C API, say, to open a database connection and then provide a sense of session over this connection?</p></li> <li><p>Could Perl help here?</p></li> </ol> <p>Many thanks for listening.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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