作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想做 cursor.mogrify
做的事,但要以适合生产的方式。
我正在更新一些通过连接字符串构建查询的遗留 Python 代码。我需要更改此设置以安全逃生。
查询很长并且构建在与运行不同的服务器上,因此使用 cursor.execute
进行转义的正常过程出于代码清晰度和实际可行性方面的原因都没有吸引力。
我会使用 mogrify,但我知道它仅用于调试目的。
我环顾四周,似乎无法找到一个好的答案。你有什么建议?
最佳答案
不要使用元组
。使用字典
d = {'p1': val1, 'p2': val2}
cur.execute("""
select *
from t
where col1 = %(p1)s and col2 = %(p2)s
""", d
)
如果有可选参数则传为null
d = {'p1': None, 'p2': val2}
cur.execute("""
select *
from t
where
(%(p1)s is null or col1 = %(p1)s)
and
(%(p2)s is null or col2 = %(p2)s)
""", d
)
建立到服务器的 ssh
连接并通过它连接。
ssh -L 5432:localhost:5432 remotehost.com
关于python - Psycopg2 生产适当的 mogrify?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20710063/
这段代码在 Java 中的等价物是什么?我放了一部分,我对 I/O 部分感兴趣: int fd = open(FILE_NAME, O_WRONLY); int ret = 0; if (fd =
我正在尝试将维度为 d1,d2,d3 的张量 M[a1,a2,a3] reshape 为维度为 d2, d1*d3 的矩阵 M[a2,a1*a3]。我试过 M.reshape(d2,d1*d3) 但是
我是一名优秀的程序员,十分优秀!