简体   繁体   English

将 SQL 粘贴到 MySQL 命令行中

[英]Pasting SQL into the MySQL command line

I have an application that is defining some SQL code:我有一个正在定义一些 SQL 代码的应用程序:

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 paste this code (everything between the double quotes) in the MySQL command line, change the one parameter and execute it, but I have run into an issue where for every line above MySQL will display:我想将此代码(双引号之间的所有内容)粘贴到 MySQL 命令行中,更改一个参数并执行它,但我遇到了一个问题,其中 MySQL 上方的每一行都将显示:

Display all 1450 possibilities?显示所有 1450 种可能性? (y or n) (y 或 n)

And then 1450 different available commands.然后是 1450 个不同的可用命令。 If I remove all linebreaks and tabs then I can paste in, but that is time consuming and a pain.如果我删除所有换行符和制表符,则可以粘贴,但这既费时又痛苦。 Is there a way that I can simply paste in the above code, edit it and then execute it as a single unit?有没有一种方法可以简单地粘贴上面的代码,编辑它然后作为一个单元执行它?

This is the default mysql (CLI) behavior each time the user presses the Tab key ( mysql uses the underlying readline or EditLine libraries (not on Windows)).这是每次用户按下Tab键时的默认mysql (CLI) 行为( mysql使用底层readlineEditLine库(不在 Windows 上))。

By default, when the user requests to use a database, mysql reads tables and fields definitions.默认情况下,当用户请求use数据库时, mysql读取表和字段定义。 Then, pressing the Tab key makes mysql conveniently offers completion of the current input with the known tables and fields.然后,按Tab键使mysql方便地提供具有已知表和字段的当前输入的完成。

However, pasting some text into mysql that contains TAB characters ( \\t or 0x09 ) triggers the same behavior - even though no Tab key was actually pressed from the keyboard.但是,将一些包含 TAB 字符( \\t0x09 )的文本粘贴到mysql中会触发相同的行为 - 即使实际上没有从键盘按下Tab键。 And this can be annoying.这可能很烦人。


Two options given to mysql can prevent that behavior, though.不过,给mysql 的两个选项可以防止这种行为。 My favorite is --disable-auto-rehash .我最喜欢的是--disable-auto-rehash The other one is --quiet or -q .另一个是--quiet-q

  • --disable-auto-rehash to prevent database, table, and column name completion (which are not read from the database, use the rehash command if later on you need completion). --disable-auto-rehash防止数据库、表和列名补全(这些不是从数据库中读取的,如果以后需要rehash ,请使用rehash命令)。 Commands history is kept, though (retrieved via the and keys for instance).但是,命令历史记录被保留(例如通过键检索)。 Which is convenient.哪个方便。

  • --quick or -q which makes mysql not using the history file and no completion (does not read the database definitions). --quick-q使mysql不使用历史文件并且不完成(不读取数据库定义)。

On Linux one may add an alias in .bashrc to use --disable-auto-rehash automatically在 Linux 上,可以在.bashrc添加别名以--disable-auto-rehash使用--disable-auto-rehash

alias mysql2='mysql --disable-auto-rehash'

Perhaps you could save the statement to a text file myTest.sql , then use the MySQL command source myTest.sql to run it?也许您可以将语句保存到文本文件myTest.sql ,然后使用 MySQL 命令source myTest.sql来运行它? You could then tweak the SQL in the file, save the changes, and run it again.然后,您可以调整文件中的 SQL,保存更改,然后再次运行它。

You need to remove the line breaks and tabs.您需要删除换行符和制表符。 The double tab is causing it to display the Display all 1450 possibilities? (y or n)双选项卡导致它显示Display all 1450 possibilities? (y or n) Display all 1450 possibilities? (y or n) and the line breaks are causing it to execute early. Display all 1450 possibilities? (y or n)和换行符导致它提前执行。

If it's PHP, write a little script to strip it for you:如果是 PHP,写一个小脚本为你剥离它:

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

Or something similar for other languages.或其他语言的类似内容。

Breaking not so bad's answer explained the cause of this problem really well.打破不那么糟糕的答案很好地解释了这个问题的原因。

From the question:从问题:

If I remove all linebreaks and tabs then I can paste in, but that is time consuming and a pain.如果我删除所有换行符和制表符,则可以粘贴,但这既费时又痛苦。

In my case, I just replaced the tabs with spaces and I was able to paste the query just fine.就我而言,我只是用空格替换了选项卡,并且能够很好地粘贴查询。 The MySQL console doesn't seem to care about the newlines, just the tabs. MySQL 控制台似乎并不关心换行符,只关心选项卡。

As a way to prevent this, most editors have a setting that will insert tabs instead of spaces when you press the Tab key.为了防止这种情况,大多数编辑器都有一个设置,当您按下Tab键时,将插入制表符而不是空格。 I normally have my IDEs configured this way, but in this instance it was a query I'd copied from MySQL workbench.我通常以这种方式配置我的 IDE,但在这种情况下,它是我从 MySQL 工作台复制的查询。 Conveniently, it also has a setting to use spaces instead of tabs:方便的是,它还具有使用空格而不是制表符的设置:

Edit > Preferences > General Editors > check Tab key inserts spaces instead of tabs > OK编辑>首选项>常规编辑器> 检查Tab 键插入空格而不是制表符> OK

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM