5

我目前使用 MySql,但更喜欢 ODBC 解决方案以使其成为未来的证明。

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

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

4

3 回答 3

8

尝试使用参数化的 SQL 语句

像这样。

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

查看 embarcadero 的这篇文章,了解有关如何使用参数的更多信息Using Parameters in Queries.

于 2011-06-01T07:55:04.797 回答
8
  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.
于 2011-06-01T09:12:27.813 回答
3

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

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

This is "my" text

将被保存为

"This is ""my"" text"
于 2011-06-01T07:48:47.613 回答