无法在 MySQL 中创建预准备语句
我正在尝试在 MySQL 中创建一个接受单个参数的准备好的语句。当我在命令行上尝试此操作时,出现语法错误。但是,当我准备好的语句中没有变量时,我可以很好地创建它们。下面是我在 MySQL 命令提示符处看到的内容的复制和粘贴:
mysql> PREPARE state_name FROM "select * from ? limit 1";
ERROR 1064 (42000): You have an error in your SQL syntax;检查与您的 MySQL 服务器版本相对应的手册,了解在 '? 附近使用的正确语法。 limit 1' at line 1
mysql> PREPARE state_name FROM "select * from documents limit 1";
Query OK, 0 rows affected (0.00 sec) Statement prepared
我使用的MySQL版本是5.0.77-log。
是否存在我没有看到的语法错误?为了让准备好的语句发挥作用,我是否必须设置任何配置参数?
I'm trying to create a prepared statement in MySQL that takes in a single parameter. When I try this on the command line I get a syntax error. However, when there are no variables in my prepared statement, I am able to create them fine. Below is a copy and paste of what I am seeing at the MySQL command prompt:
mysql> PREPARE state_name FROM "select * from ? limit 1";
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? limit 1' at line 1
mysql> PREPARE state_name FROM "select * from documents limit 1";
Query OK, 0 rows affected (0.00 sec) Statement prepared
The version of MySQL I'm using is 5.0.77-log.
Is there a syntax error I'm not seeing? And are there any config parameters I have to set in order to get prepared statements to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
恐怕您不能将表名作为准备好的语句参数传递,也不能传递列进行分组或排序。您唯一可以参数化的是 where 子句或更新中的字段:
You can't pass in table names as prepared statement parameters, I'm afraid, neither can you pass columns to group or sort by. The only thing you can parametrize are fields in where clauses or for updates:
如果您想创建一个准备语句并将其用于多个表,您实际上可以为每个表创建一个变量,然后使用该变量创建语句,如 MySQL 手册 解释一下:
If you want to create a single prepare statement and use it for more than one table, you cand actually create a variable for every table, and then create the statement using this variable, as the MySQL manual explain it :