如何让 PyCharm 识别 SQL 语句中的占位符? [英] How do I make PyCharm recognize placeholders in SQL statements?
问题描述
我正在使用 PyCharm,但在此 SQL 语句中出现错误:
I'm using PyCharm and I have an error on this SQL statement:
SELECT * FROM table WHERE id= %(id)s
代码语法检查说:
<expression> expected, got '%'
有没有办法禁用此错误消息?换句话说,我如何让 PyCharm 识别 SQL 语句中的占位符?
Is there a way to disable this error message? In other words, how do I make PyCharm recognize placeholders in SQL statements?
编辑
这是我的解决方案:
对于 %(id)s
和 %s
你必须有 %\((\w+)\)s
和 %s
在 Settings/Preferences |工具 |数据库 |用户参数
并选择所有语言
不仅仅是SQL
For %(id)s
and %s
you have to had %\((\w+)\)s
and %s
in Settings/Preferences | Tools | Database | User parameters
and select All languages
not only SQL
推荐答案
这个 SQL 是在 SQL 控制台中,还是在 Python 脚本中的字符串中?我遇到了同样的问题,并通过选中模式列表上方的框解决了它,该框标记为使用 SQL 注入在字符串文字中启用".如果您在 SQL 控制台窗口中执行此操作,请选中第一个框.
Is this SQL in a SQL console, or in a string in a Python script? I had this same issue and solved it by checking the box above the list of patterns, labeled "Enable in string literals with SQL injection". If you're doing this in a SQL console window, check the first box.
正如@LazyOne 所指出的,转到 Settings/Preferences >工具 >数据库用户参数
,您会在顶部看到两个复选框:在控制台和 SQL 文件中启用
在字符串文字中启用 SQL 注入
As @LazyOne notes, go to Settings/Preferences > Tools > Database > User Parameters
and you'll see two checkboxes at the top:
Enable in console and SQL files
Enable in string literals with SQL injection
虽然您可以添加自己的自定义模式,但我相信与此参数语法(对我而言,用于 Postgres 的 psycopg2 的参数)匹配的模式在 Pycharm 中是开箱即用的,是列表中的最后一个.
While you can add your own custom patters, I believe the pattern that matches this parameter syntax (for me, parameters for psycopg2 for use in Postgres) is out-of-the-box in Pycharm, the last one in the list.
请参阅此处以供参考:
https://www.jetbrains.com/help/pycharm/2016.1/user-parameters.html
这篇关于如何让 PyCharm 识别 SQL 语句中的占位符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!