Q:

Problems trying to use MySQL in conjunction with Emacs

I'm running into a problem trying to use MySQL in conjunction with Emacs.  Let me start by giving the relevant version information:
Local OS: Windows XP, SP3
Terminal emulator/SSH client: Putty 0.58
Remote OS (as reported by uname -a): Linux 2.6.19.7 (also happens on another installation whose Linux is 2.6.18)
Emacs version (running on remote OS): 21.3.1 (also happens on another installation whose Emacs is 21.4.1)
MySQL version (running on remote OS): 4.1.22 (also happens on another installation whose MySQL is 5.0.91)

I always have a shell buffer running inside Emacs, and I'd like to run the command 'mysql' so I can run SQL queries.  But if I type
mysql -uuserid -ppassword

it just hangs.  When I kill the mysql process, it reports:
bash: [XXXXX: 2] tcsetattr: Invalid argument
    (where XXXXX is the process id of the bash shell that's the parent of the mysql process that was killed.)

Years ago I wrote C code that called tcsetattr() to set flags for the tty device driver.  I remember it being based on the idea that a tty was connected via RS-232, and many of the attributes you set via tcsetattr() controlled characteristics of the RS-232 connection - baud rate, parity, etc.  Obviously, for pseudo-ttys, most of that is irrelevant.  It looks like the 'mysql' executable is trying to do some hardware control that doesn't work in the context of an Emacs shell buffer.
As I investigated further, I discovered that Emacs has a Lisp function named sql-mysql which sounds like it runs MySQL queries directly.  That sounded good to me.  But when I tried it I ran into different problems.
What little documentation I could find on how to use M-x sql-mysql was rather incomplete.  It didn't give any information on how to run a query.  http://www.emacswiki.org/emacs/SqlMode says,
"Type M-x sql-help RET to read the online help."

so I did that.  The help suggests running:
M-x sql-mysql

to invoke MySQL, and then says,
"Once you have the SQLi buffer, you can enter SQL statements in the buffer.  The output generated is appended to the buffer and a new prompt is generated."

The buffer never displayed a prompt.  I can't click on the items in Emacs' menubar because I'm connecting via ssh (client is putty), and putty doesn't seem to transmit mouse clicks.  So I can't get at the "SQL" or "In/Out" menus in Emacs' menubar.
I tried sending a C-c (by typing C-c C-c) to the *SQL* buffer, expecting that would cause it to exit, just like a shell buffer.  Much to my surprise, IT DISPLAYED THE OUTPUT OF THE QUERY I'D ENTERED before reporting:
Ctrl-C -- exit!
Process SQL exited abnormally with code 1

I then noticed that M-x sql-help also says:
"If you have a really complex SQL statement or if you are writing a procedure, you can do this in a separate buffer. Put the new buffer in `sql-mode' by calling M-x sql-mode.  The name of this buffer can be anything.  The name of the major mode is SQL."

"In this SQL buffer (SQL mode), you can send the region or the entire buffer to the interactive SQL buffer (SQLi mode).  The results are appended to the SQLi buffer without disturbing your SQL buffer."

So I created a second buffer, named it "my-sql-buffer" and ran M-x sql-mode.  When I type the query:
show tables;

into that buffer and terminate it with RET, nothing happens in the *SQL* buffer.  I tried positioning my cursor at the end of the query and typing C-c C-c.  The minibuffer at the bottom displayed:
"Sent string to buffer *SQL*."

but nothing appeared in the *SQL* buffer.  I typed C-c C-c a few more times.  Each time the minibuffer redisplayed:
"Sent string to buffer *SQL*."

After typing C-c C-c several times, suddenly output appeared in the *SQL* buffer.  It was the output from:
show tables;

repeated 5 times!  Except the last time stopped in the middle.  This behavior suggests that something is buffering the output.  I checked how many bytes had been output (C-x h M-C-| wc -c) and it had output 4096 bytes!  Since 4K is a common buffer size, this confirms my suspicion that something is buffering the output.  But I can't figure out what's doing the buffering and how to turn the buffering off.
Can anyone help me solve one or more of the following problems:

How to run the mysql executable from a bash prompt inside emacs and not have it hang when it calls tcsetattr()?
How to run M-x sql-mysql, type a query into the *SQL* buffer and actually get it to run without making the SQL process exit?
How to flush the output to *SQL* after each query when I type a query into a second buffer that's in sql-mode?

advTHANKSance for your help.
Mark Rosenthal

A:

You got to add (setq sql-mysql-options '("-C" "-t" "-f" "-n")) to your _emacs to make sql-mysql work on Windows properly.  M-x describe-variable sql-mysql-options to read the docs.

