Insert sql into mySql command line?

I have an application that defines some sql code:

mySql = "SELECT sq.question, qs.title, sq.id as question_id, sq.type, qs.id as option_id, sri.title as rankTitle, sri.id as rankId, sfi.title as formTitle, sfi.id as formId, sq.sub_type, sq.sort_order FROM survey_questions as sq LEFT JOIN question_suboptions as qs ON sq.id = qs.question_id LEFT JOIN survey_rankingitems as sri ON sq.id = sri.question_id LEFT JOIN survey_formitems as sfi ON sq.id = sfi.question_id WHERE sq.survey_id = #{@surveyId} ORDER BY sq.sort_order" 

I would like to insert this code (everything between double quotes) in the mySql command line, change one parameter and execute it, but I ran into a problem when for each line above mySql would display:

Show all 1450 features? (y or n)

And then 1450 different teams available. If I delete all the lines and tabs, I can insert them, but it takes a lot of time and pain. Is there a way that I can simply paste the above code, edit it and execute it as a unit?

+6
source share
3 answers

Perhaps you could save the statement in the text file myTest.sql and then use the MySQL source myTest.sql to run it? Then you can configure SQL in the file, save the changes and run it again.

+3
source

(The answer to the old question is May 2015)

This is the default mysql (CLI) behavior every time the user presses the TAB key (mysql uses the basic readline libraries or the EditLine libraries [not on Windows]).

By default, when a user queries use database, mysql reads table and field definitions. Then, pressing the TAB key allows mysql to conveniently complete the current input using known tables and fields.

However, inserting some text in mysql that contains TAB characters ( \t or 0x09 ) causes the same behavior - even if the TAB key is actually pressed from the keyboard. And it can be annoying.


Two options given by mysql can prevent this behavior. My favorite is --disable-auto-rehash . The other is --quiet or -q .

  • --disable-auto-rehash to prevent the completion of filling the database, table and column (which cannot be read from the database, use the rehash command if you need to complete later). The history of the commands is saved, though (for example, using the and keys). It's comfortable.

  • --quick or -q , which causes mysql not to use the history file and not execute (does not read database definitions).

On Linux, you can add an alias to .bashrc to automatically use --disable-auto-rehash

 alias mysql2='mysql --disable-auto-rehash' 
+10
source

You need to remove line breaks and tabs. Does double tab cause Display all 1450 possibilities? (y or n) Display all 1450 possibilities? (y or n) , and line breaks force it to execute earlier.

If this is PHP, why not write a little script to remove it for you?

 echo (preg_replace("/\s+/", " ", $string)); 

Or something similar for other languages.

+2
source

All Articles