简体   繁体   English

将格式化和突出显示的多行文本 (SQL) 粘贴到 PyCharm 中的字符串文字中

[英]Paste a formatted and higlighted multiline text (SQL) into string literal in PyCharm

Is there a way in PyCharm to paste multiline text (SQL) into string literal? PyCharm 中有没有办法将多行文本(SQL)粘贴到字符串文字中? Eg:例如:

SELECT column1
     , column2
FROM table1
WHERE column3 IN
(
    SELECT TOP(1) column4
    FROM table2
    INNER JOIN table3
    ON table2.column1 = table3.column1
)

Into:进入:

sql = (
    "SELECT column1"
    "     , column2"
    "FROM table1"
    "..."
)

And ideally tell PyCharm it is text in SQL language to highlight syntax?理想情况下告诉 PyCharm 它是 SQL 语言中的文本以突出显示语法?

Is there a way in PyCharm to paste multiline text (SQL) into string literal? PyCharm 中有没有办法将多行文本(SQL)粘贴到字符串文字中?

Yes.是的。 First you'll have to "Create a live template" and since you want to paste the right function to use would be clipboard() .首先,您必须“创建一个实时模板” ,因为您要粘贴正确的 function 以使用clipboard() You can do this by going to File > Settings > Editor > Live Templates as shown in the screenshot.您可以通过转到File > Settings > Editor > Live Templates来执行此操作,如屏幕截图所示。

在此处输入图像描述

And ideally tell PyCharm it is text in SQL language to highlight syntax?理想情况下告诉 PyCharm 它是 SQL 语言中的文本以突出显示语法?

Yes.是的。 You can do this by using "language injection" in the code segment.您可以通过在代码段中使用“语言注入”来做到这一点。 If you have it preconfiguered like shown in the following screenshot PyCharm should autodetect SQL and apply the syntax highlight.如果您已像以下屏幕截图所示进行预配置,PyCharm 应自动检测 SQL 并应用语法高亮显示。 It can be set at File > Settings > Editor > Intention .它可以在File > Settings > Editor > Intention中设置。

在此处输入图像描述

If you don't have the intention set to autodetect you can also do it manually by right-clicking and choosing "Show Context Actions" or pressing the light-bulb in the code (this is made easier by putting the SQL code inside a string).如果您没有将意图设置为自动检测,您也可以通过右键单击并选择“显示上下文操作”或按下代码中的灯泡手动执行此操作(通过将 SQL 代码放入字符串中,这更容易)。

在此处输入图像描述

After choosing the SQL dialect you prefer, you can also fine-tune syntax highlight and syntax checks of the injected language.选择您喜欢的 SQL 方言后,您还可以微调注入语言的语法高亮和语法检查。

Functionalities功能 IDE path to dialogue IDE 对话路径
Syntax checks语法检查 Settings > Editor > Inspections > SQL Settings > Editor > Inspections > SQL
Syntax highlights语法亮点 Settings > Editor > Color Sheme > SQL Settings > Editor > Color Sheme方案> SQL

For multiline SQL queries, in Python, you can use triple-quote:对于多行 SQL 查询,在 Python 中,您可以使用三引号:

query = """
SELECT column1
     , column2
FROM table1
WHERE column3 IN
(
    SELECT TOP(1) column4
    FROM table2
    INNER JOIN table3
    ON table2.column1 = table3.column1
)
"""

For syntax highlight, however, since PyCharm is build for Python specifically, I don't think you can change the settings to highlight like in SQL.然而,对于语法高亮,由于 PyCharm 是专门为 Python 构建的,我认为您不能像在 SQL 中那样更改设置以突出显示。 There may be extensions for this, however, I do not know of any.可能会有扩展,但是,我不知道。

using """ """使用""" """

sql = """
        SELECT column1
     , column2
FROM table1
WHERE column3 IN
(
    SELECT TOP(1) column4
    FROM table2
    INNER JOIN table3
    ON table2.column1 = table3.column1
)
"""

print(sql)

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

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