Perl dbi prepare 引用错误

Perl dbi prepare is putting wrong quote

我的代码与下面类似:

$sth = $dbh->prepare("CREATE TABLE IF NOT EXISTS ? (`id` bigint(100) unsigned NOT NULL AUTO_INCREMENT");
$sth->execute('test');

我遇到了错误:

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 ''test'

从跟踪中我发现 perl DBI 在 table 名称周围放置了单引号,mysql 不喜欢。如何告诉 DBI 不要放置任何引号或放置 (`) 而不是单引号?

您不能为 table 或列名使用占位符。 PreparedStatement 会将此值解释为字符串并在其周围加上单引号。

它只是按照你的要求去做。当给定一个字符串时,? 相当于一个字符串文字。所以

SELECT * FROM Table WHERE field = ?

表示

SELECT * FROM Table WHERE field = 'test'

SELECT * FROM ?

表示

SELECT * FROM 'test'

您需要使用

$dbh->prepare("
   CREATE TABLE IF NOT EXISTS ".( $dbh->quote_identifier('test') )." (
             `id` bigint(100) unsigned NOT NULL AUTO_INCREMENT
          )
");