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 通信,那么您应该调用库中向您公开这些相同功能的方法。