简体   繁体   English

如何在perl中引用查询字符串?

[英]how to quote string for queries in perl?

I need to match large strings with the fields of a column in mysqlDB. 我需要将大字符串与mysqlDB中一列的字段匹配。 something like 就像是

%ERROR-kkl_ub{}: (from YAMios): Destination (name = sdasdays.s0sda.rindasdth.sbo98.sbssdao_sad) is not registered. %ERROR-kkl_ub {} :(来自YAMios):未注册目的地(名称= sdasdays.s0sda.rindasdth.sbo98.sbssdao_sad)。

it contains a lot of special characters, i cannot try and do a \\ for each of them as there will be hundreds of unique signatures. 它包含许多特殊字符,我无法尝试为每个字符做一个\\,因为会有数百个独特的签名。

need help... 需要帮忙...

In perl, us literal quotes, with single quote characters, or the q operator, which was designed for quoting literal strings which may have the normal quote characters often. 在perl中,我们使用带单引号字符的文字引号或q运算符,该操作符旨在引用可能经常具有普通引号字符的文字字符串。 q (and qq for interpolated strings) allows for the next character to be the bounding quote character (similar to the m and s regular expression operators). q(对于内插字符串,为qq)允许下一个字符成为边界引号字符(类似于m和s正则表达式运算符)。

my $var1 = 'this is a "literal" string';
my $var2 = "this is an \"interpolated\" string";
my $var3 = q/this is a "literal" 'string'/;
my $var4 = q{another literal string};
my $var5 = qq/interpolated string, previous one was '$var2'/;

If you are worried about getting the strings into SQL queries (I admit I found your wording slightly confusing), use bound variables. 如果您担心将字符串放入SQL查询中(我承认我发现您的措辞有些混乱),请使用绑定变量。

my $dbh = DBI->connect( ... );
my $sth = $dbh->prepare('SELECT * FROM table WHERE id = ? OR string_field = ?');
my $rv = $sth->execute(1425, $var5);

See the DBI perldoc for more info. 有关更多信息,请参见DBI perldoc。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM