使用Perl在mysql中使用空格查询字符串

sma*_*ape 2 mysql perl full-text-search dbi

我认为这个问题已经被问到了php,现在我要求它用于perl.我有2个阵列.我用它来查询我的数据库.现在碰巧这些术语不仅仅是一个单词,因此它可能有空格.

我声明了2个变量,比如$ foo和$ bar.我有2个for循环,它将文件1中的每个术语组合到文件2中的每个术语并查询数据库.每次每个术语都进入变量.数据库是文本索引的.我正在使用DBI模块.我的查询是这样的:

my $query_handle = $connect->prepare("SELECT value FROM value_table WHERE
MATCH(column_text_indexed) AGAINST ('+"$foo" +"$bar"' in boolean mode)") || die 
"Prepare failed:$DBI::errstr\n";
Run Code Online (Sandbox Code Playgroud)

它给出以下错误:

Scalar found where operator expected at program.pl line 32, near ""SELECT value FROM value_table
WHERE MATCH(column_text_indexed) AGAINST ('+"$foo"
        (Missing operator before $foo?)
String found where operator expected at program.pl line 32, near "$foo" +""
    (Missing operator before " +"?)
Scalar found where operator expected at program.pl line 32, near "" +"$bar"
    (Missing operator before $bar?)
String found where operator expected at program.pl line 32, near "$bar"' in boolean mode)""
    (Missing operator before "' in boolean mode)"?)
syntax error at program.pl line 32, near ""SELECT value FROM value_table WHERE
MATCH(column_text_indexed) against ('+"$foo"
Execution of program.pl aborted due to compilation errors.
Run Code Online (Sandbox Code Playgroud)

另外,我的另一个问题是:当术语像"81-α-羟化酶缺乏症"或"26-脱氧活性稳定"时,是否正确使用这些术语的引号?根据http://dev.mysql.com/doc/refman/5.5/en/fulltext-boolean.html,如果搜索确切的术语并且我想要搜索确切的术语,它会说使用引号.当我使用()时,我得到了结果但是我认为它将整个单词用空格分隔为不同的单词,并给出任何一个单词的结果.

任何帮助是极大的赞赏.

谢谢.

ike*_*ami 5

假设您要创建字符串

+"foo" +"bar"
Run Code Online (Sandbox Code Playgroud)

(是的,我现在忽略了插值.)

您不能将整个事物放在双引号中,因为字符串文字将在遇到的第一个双引号处结束.您还需要转义现有的双引号(以及双引号字符串中特殊的任何其他字符).

"+\"foo\" +\"bar\""
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用不同的分隔符

qq!+"foo" +"bar"!
Run Code Online (Sandbox Code Playgroud)

要么

qq{+"foo" +"bar"}
Run Code Online (Sandbox Code Playgroud)

现在让我们插入我们想要的值.

qq{+"$foo" +"$bar"}
Run Code Online (Sandbox Code Playgroud)

全部一起:

my $sth = $dbh->prepare(qq{
   SELECT value
     FROM value_table
    WHERE MATCH (column_text_indexed) AGAINST (? IN BOOLEAN MODE)
});

$sth->execute(qq{+"$foo" +"$bar"});
Run Code Online (Sandbox Code Playgroud)

注意使用占位符!始终使用占位符.(如果您还不知道,请在DBI文档中搜索该单词.)如果$foo$bar包含双引号或其他奇怪的字符,您的数据将不会被意外解释为代码.