无法在MySQL中创建准备好的语句 [英] Unable to Create Prepared Statements in MySQL

查看:50
本文介绍了无法在MySQL中创建准备好的语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在MySQL中创建一个带单个参数的准备好的语句.当我在命令行上尝试此操作时,出现语法错误.但是,当我准备好的语句中没有变量时,我可以很好地创建它们.以下是我在MySQL命令提示符下看到的内容的副本和粘贴:

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";

错误1064(42000):您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以获取在'?附近使用的正确语法.在第1行限制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

我正在使用的MySQL版本是5.0.77-log.

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?

推荐答案

恐怕您不能将表名作为准备好的语句参数传递,也不能将列传递给分组或排序依据.您唯一可以参数化的是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:

PREPARE state_name FROM "select * from documents where id = ?";

PREPARE state_name FROM "update documents set field = ? where id = ?";

这篇关于无法在MySQL中创建准备好的语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆