mysql - 准备好的语句的内部结构是什么样的?

标签 mysql sql ruby sqlite dbi

<分区>

bind_params 似乎是如何准备 sql 语句的:

stmt = db.prepare( "select * from table where a=? and b=?" )
stmt.bind_params( 15, "hello" )

所以实际上在 stmt 内部,我们需要有映射/数组或最终映射参数并创建正确的 stmt 的东西。在内部执行此操作的最佳方法是什么?另外,我想字符串需要额外的预防措施——上面的内容必须映射为“select * from table where a = 15 and b =\"hello\"”。

我查看了 SQLite3 和 OCI,它们似乎将这些传递给内部 C 代码。

最佳答案

I am trying to prepare the queries at the client side and send it to the server

如果您正在尝试做听起来像是您正在尝试做的事情...请不要尝试那样做。

这不是准备好的语句(或者至少不是它应该的样子)。

您的客户端代码不应尝试将值插入查询字符串以生成“已完成”的查询以发送到服务器执行。这是灾难的根源,更不用说虚假的安全感了。

准备好的语句将带有 ? 占位符的语句按原样发送到服务器,服务器在此处“准备”要执行的语句...然后客户端将参数发送到服务器 ("绑定(bind)”参数)以执行。这样做,服务器将永远不会混淆“哪部分是 SQL”和“哪部分是数据”,从而使 SQL 注入(inject)变得不可能,并且无需转义和清理数据。

mysql_stmt_bind_param() is used to bind input data for the parameter markers in the SQL statement that was passed to mysql_stmt_prepare(). It uses MYSQL_BIND structures to supply the data. bind is the address of an array of MYSQL_BIND structures. The client library expects the array to contain one element for each ? parameter marker that is present in the query.

http://dev.mysql.com/doc/refman/5.6/en/mysql-stmt-bind-param.html

如果您不直接与 C-API 通信,那么您应该调用库中向您公开这些相同功能的方法。

关于mysql - 准备好的语句的内部结构是什么样的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20034845/

相关文章:

php - 如何将大型 sql 文件导入 mysql 表

mysql - 不从 MySQL 中选择重复项

ruby-on-rails - 如何使用新属性扩展 ActiveRecord 模型?

php - 在 MySQL 中存储任意字符编码的任意字符串并检索它们

java - 如何将我的结果集移动到特定记录

mysql - 在 MySQL 存储函数中对 CHAR 使用 Case 语句

ruby-on-rails - simple_form仅在存在文件时才允许上传

mysql - 拉拉贡 :How to import large mysql dump

php - Select date and set it in other language-multiple queries in single mysql statement in PHP in single mysql statement 多查询

mysql - ROR : How to sort array with . 限制方法