- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不知道如何最好地表达这个问题,但我会尝试。
我有几个通过 crontab 执行的 python 脚本。这些脚本执行一些数据库操作,例如选择和更新(在不同的数据库上),并且有些脚本使用相同的 SQL 语句。就目前情况而言,我使用了 SQL 查询,存储在 util.py 文件中的静态字典中,并且每当需要时我都会从不同的脚本中导入它,但是我想知道将静态 SQL 语句存储到的最佳实践是什么被不同的脚本使用?可读性很重要,因此我需要某种可以正确缩进 SQL 查询的文件格式。
最佳答案
3 个选项。
(注意:下面的 select * from t where custid = %(custid)s
之类的内容稍后将被馈送到 postgresql 参数化查询。使用 RDBMS 的参数化查询语法,切勿手动构建查询字符串 because sql injection一个非常好的理由。即使如此,也将其限制为来自应用程序本身的配置 - 位于文件系统某处的外部“数据源文件”可能会被不良行为者损坏,然后可以搭上您的应用程序凭据的便车。
我会使用旧的 INI/ConfigParser 格式。以前经常使用它们,但现在更倾向于 json,只不过 json 对于 sql 来说不太好。
这是Python
import ConfigParser
cfp = ConfigParser.ConfigParser()
cfp.read("test_so08.ini")
myselect = cfp.get("sql", "select", raw=True)
for line in myselect.split("\n"):
print ":%s:" % (line)
还有ini。请注意,您需要在 select= 之后的每个后续行中至少缩进一个空格,否则 configparser 将尝试解析其他行,而不是将它们与 select 分组。
[sql]
select=select *
from customers
where custid = %(custid)s
select2=<some other stuff>
打印输出:
:select *:
:from customers:
:where custid = %(custid)s:
优点:它不是代码文件,因此理论上您可以让任何人配置 sql。理论上。
缺点:基于ini的sql在获取变量方面有相当大的开销,并且当您想要添加更多功能时,您最终会做很多解决方法。
另一种选择是仅使用 Python 模块并使用三引号字符串。现在我倾向于将其用于 sql,而不是 ini。
优点:简单。
缺点:仅限 Python,无效的语法拼写错误将停止您的调用脚本。
say this is sql_test_so08.py
select = """
select *
from customers
where custid = %(custid)s
"""
select2 = """<some other stuff>"""
在你的脚本中:
import sql_test_so08
print sql_test_so08.select
一个优点是我可以使用 Template/$var 替换来替换从其他地方导入的应用程序常量,而不是在 sql 中将它们硬编码。假设您将 invalid_customer_unpaid 标志设置为 3。
在 sql.py 文件中你会喜欢:
from constants import invalid_customer_unpaid
di_constants = dict(invalid_customer_unpaid=invalid_customer_unpaid)
select_bad="""select... where ... $invalid_customer_unpaid"""
select_bad = Template(select_bad).substitute(di_constants)
注意:我很少使用直接变量替换,如此处所示。 SQL 注入(inject)的风险太大。但是您可以使用 RDBMS 的绑定(bind)参数支持来应用相同的想法。
这些天做了更多的 YAML。
给定test.yaml:
sql:
select: select * from customers where custid = %(custid)s
text: 'some text with ''abc'' and "xyz" '
然后
from yaml import safe_load as yload
with open("test.yaml") as fi:
di2 = yload(fi)
print (di2["sql"]["select"])
将输出:从 custid = %(custid)s 的客户中选择 *
关于python - 将 SQL 语句存储在属性文件中以供 Python 脚本使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30218963/
大家好我有一个应用程序可以打开一个包含文本和图像组合的文章的 WebView 。如您所知,文章有不同的字体,我想知道是否有办法让 Web View 呈现几种 Helvetica 字体,就像在安装了这些
我正在寻找一种好的格式来归档旧 Linux 计算机的整个文件系统。 西藏自治区 tar.gz 格式非常适合归档具有 UNIX 样式属性的文件,但由于压缩应用于整个归档,因此设计排除了随机访问。相反,如
我最近一直在苦苦挣扎,因为我不得不更改我不久前编写的一些代码以在 Qt 中进行图像处理和 OpenGl以支持多线程。 问题是我想用它在一组图像上应用批量过滤器, 我正在使用 openMP 来做这样的多
我已成功将两个实际设备连接到 Azure IoTHub(在同一个 IoT 中心),并希望第二个设备接收第一个设备发送的消息。因此,在普通的 MQTT 代理中,第二个设备仅订阅该主题,但 Azure没有
我需要一些 Jenkins 作业才能通过 ssh 通过 shell 命令行访问其他一些机器。 我该怎么做? 我没有目标服务器的密码,但我有一个“ key ”文件,但是当我使用以下命令运行作业时 ssh
我在 Mac OSX Mavericks 上使用 MAMP。我想安装最新的 XDebug v2.2.4。根据 XDebug 向导,我下载了 XDebug 2.2.4 源代码。我的系统上安装了许多版本的
所以我正在制作一个程序,从用户给定的 AZ Lyrics 歌曲中提取歌词。我遇到的问题是,在将字符串转换为 URL 后,它说 Jsoup 无法解析它,因为它不接受字符串,尽管变量是我们传入的 URL。
我已经安装了 AWS .NET SDK通过 MSI 安装程序。我想使用这段代码: static IAmazonS3 client; client = new AmazonS3Client(Amazon
我正在使用 cURL 检索另一个页面,除非我有特定的 cookie,否则我看不到页面内容。 cookie 名称是 seepage,它的值必须设置为 1 才能让我看到页面内容。 我想使用 cURL 加载
目录 1、VLC代码封装 1.1 QT(C++)工程 1.2static 声明 m_instance 优化效率 1.3封装 DLL
我是 Docker 新手,但有一个 Java Web 应用程序项目,我的 Maven 构建会为其生成并安装 Docker 镜像。即 mvn clean install 产品: REPOSITORY
我正在尝试在 Debian Stretch 上做一个带有 uinput 的虚拟键盘,我可以输入字符串,比如“Toto!”,键盘会写入这个字符串。但是,我一直坚持从 C char 到键盘处理的键码的转换
我们正在 Rails 中开发一个 JSON REST API,供我们也在开发的 Android 应用程序使用。有什么方法可以保护 API,使其只能由我们特定的 Android 应用程序使用? API
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
所以我有一个问题,我有 2 个应用程序部署在具有不同端口的同一网站上,一个是登录应用程序,一个是HRIS 系统。 我的问题是,当我的登录应用程序创建 token 时并重定向到我的 hris 系统,hr
在给出的第三段代码的上下文中交谈 on this page , 有一个方法 Messenger named getBinder()返回 the IBinder Messenger 用于与 associ
我目前有一个私有(private) Java 项目,我正在尝试将其完善以供其他开发人员使用。我想做的一件事是让 Ant 自己下载 JUnit、PMD 和 FindBugs 等所需的 JAR,这样开发人
我想在 Qt 中使用 ffmpeg 库进行编程。 如何在 Windows 上将 ffmpeg 编译成 *.lib 文件? 或 如何使用Qt官方发布的编译后的*.dll文件? 还有,哪种方式比较好? 最
我正在为扩展 std::vector 实例化的 C++ 类开发 Python 绑定(bind)。为了让 Python 下标运算符为此类工作,我添加了如下所示的 __getitem__ 函数(我删除了不
我正在为 Amazon SWF 的相当简单的工作流程编写 PHP。我发现自己开始编写一个库来检查某些操作是否已经开始或完成。基本上遍历事件列表以检查事情的进展情况,然后在需要时启动适当的事件。有时这可
我是一名优秀的程序员,十分优秀!