如何清理 ODBC 数据库输入?

发布于 2024-11-10 12:35:29 字数 169 浏览 11 评论 0原文

我目前使用 MySql,但更喜欢 ODBC 解决方案以使其面向未来。

在将用户输入传递到 ODBC 数据库之前如何对其进行清理?

而且,当我这样做时,我将字符串用双引号括起来,例如“INSERT INTO VALUES(description) ""` - 但是如果文本本身包含双引号怎么办?

I currently use MySql, but would prefer an ODBC solution to make it future proof.

How do I sanitize user input before passing it to an ODBC database ?

And, while I'm at it, I wrap my string in double quotes, e.g. "INSERT INTO VALUES(description) ""` - but what if the text itself contains a double quote?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

难以启齿的温柔 2024-11-17 12:35:29

尝试使用像这样的参数化 SQL 语句

INSERT INTO MyTable (Field1,Field2) VALUES (:Param1,:Param2)

查看 embarcadero 上的这篇文章,了解有关如何使用参数的更多信息在查询中使用参数。

Try using a parametrized SQL sentence

like this.

INSERT INTO MyTable (Field1,Field2) VALUES (:Param1,:Param2)

check this article from embarcadero for more info about how use parameters Using Parameters in Queries.

终难遇 2024-11-17 12:35:29
  1. ODBC 不是使用 MySQL 的最佳方式。即使您将来需要支持很少的 DBMS,那么您也可以考虑多 DBMS 数据访问库,包括 dbExpress(Delphi 自带)和 3d party - AnyDAC(商业)、ZeosLib(免费软件)等。
  2. 如果您需要将字符串常量替换为 MySQL 查询,那么您需要 转义特殊字符 或将字符串转换为 十六进制表示。这可以保护您免受可能的 SQL 注入和语法错误的影响。但会使您的查询准备更加复杂。
  3. 最好的方法 - 使用参数并提交文字作为参数值。这既简单又安全。
  1. ODBC is not an optimal way to work with MySQL. Even if you need to support few DBMS in the future, then you can consider multi-DBMS data access libraries, including dbExpress (comes with Delphi) and 3d party - AnyDAC (commercial), ZeosLib (freeware), etc.
  2. If you need to substitute a string constant into a MySQL query, then you need to esacape the special characters or convert the string into hexadecimal representation. That protects you from possible SQL injection and syntax errors. But makes your query preparation more complex.
  3. The best way - use parameters and submit literals as the parameter values. That is simple and safe.
苍暮颜 2024-11-17 12:35:29

如果可以的话,使用hibernate,或许可以通过delphi 中的RMI。尽管它以 java 为中心,但它几乎将程序员与底层数据库完全隔离,并处理您提到的问题以及更多问题。

顺便说一句,要回答您关于双引号的问题,要保存包含双引号的值,将它们转义为双双引号,例如

This is "my" text

将保存为

"This is ""my"" text"

Use hibernate if you can, perhaps via RMI from delphi. Although it's java centric, it almost completely insulates the programmer from the underlying DB, and handles the issues you've mentioned and a whole lot more.

btw, to answer your question re double quotes, to save a value which contains double quotes, escape them as doubled double quotes, eg

This is "my" text

would be saved as

"This is ""my"" text"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文