查询名称中的perl DBI逗号

时间:2014-07-18 08:50:57

标签: perl sqlite dbi

我正在使用perl与sqlite数据库交谈,并且收到错误。我认为这是因为一些化学名称(存储在变量$ drugs中)中有逗号(它们还有许多其他'奇怪的字符'。

任何帮助将不胜感激,谢谢。

**error message:** 
- read in   57to find CIDs for
- Opened database successfully
- retrieving results for    cyclo(L-Val-L-Pro)
- retrieving results for    Sinapic_acid
- retrieving results for    NHQ
- DBD::SQLite::db prepare failed: near ",": syntax error at get_drugName2IDinfo.sqlite.pl line 33.
- DBD::SQLite::db prepare failed: near ",": syntax error at get_drugName2IDinfo.sqlite.pl line 33.

第33行:

my $stmt = qq(SELECT * from chem_aliases WHERE alias LIKE '$drug%');

示例药物名称:

(2R,3R,4S,6R)-2-((5-hydroxy-2,2-dimethyl-3,4-dihydro-2H-benzo[h]chromen-6-yl)oxy)-6-(hydroxymethyl)tetrahydro-2H-pyran-3,4,5-triol

部分脚本:

my %HoDrugs;
while (my $line=<IN>){
    chomp $line;
    $HoDrugs{$line}=1;
}
close(IN);
print "read in\t".scalar(keys %HoDrugs)."to find CIDs for\n";
##
my $driver   = "SQLite"; 
my $database = "/Users/alisonwaller/Documents/Typas/ext_data/STITCHv3.1/STITCHv3.1.sqlite.db";
my $dsn = "DBI:$driver:dbname=$database";
my $userid = "";
my $password = "";
my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 }) 
                  or die $DBI::errstr;

print "Opened database successfully\n";
###
my $outfile="$in_drugNms.sq.plsCIDs.tab";
open (OUT,">",$outfile);
foreach my $drug (keys %HoDrugs){

    my $stmt = qq(SELECT * from chem_aliases WHERE alias LIKE '$drug%');
    my $sth = $dbh->prepare( $stmt );
    my $rv = $sth->execute() or die $DBI::errstr;
    if($rv < 0){
            print $DBI::errstr;
    }
    while(my @row = $sth->fetchrow_array()) {
            print "retrieving results for\t$drug\n";                
            print OUT join("\t",$drug,$row[0],$row[1],$row[2]) . "\n";
    }
}
    print "Operation done successfully\n";
    $dbh->disconnect();

2 个答案:

答案 0 :(得分:2)

您是否尝试过使用占位符而不仅仅是自己引用字符串?

my $sth = $dbh->prepare( 'SELECT * from chem_aliases WHERE alias LIKE ?' );
my $rv = $sth->execute( $drug . '%' ) or die $DBI::errstr;

答案 1 :(得分:0)

在执行查询以尝试删除标点字符之前,您总是可以尝试使用 $drug =~ s/[[:punct:]]//g; 吗?

如果您不想这样,可以用空格替换它们? $drug =~ s/,/ /g;