简体   繁体   English

我如何在perl脚本中的sql文件中传递perl变量

[英]how can i pass perl variable in sql file in perl script

here i have 3 variable in perl and im passing those variable in one db.sql file in sql query line then executing that db.sql file in same perl file but it's not fetching those variable. 在这里,我在perl中有3个变量,并且我在sql查询行中的一个db.sql文件中传递了这些变量,然后在同一个perl文件中执行了该db.sql文件,但未获取这些变量。

$patch_name="CS2.001_PROD_TEST_37987_spyy";
$svn_url="$/S/B";
$ftp_loc="/Releases/pkumar";
open(SQL, "$sqlfile") or die("Can't open file $sqlFile for reading");
while ($sqlStatement = <SQL>) 
{
   $sth = $dbh->prepare($sqlStatement) or die qq("Can't prepare $sqlStatement");

   $sth->execute() or die ("Can't execute $sqlStatement");
}
close SQL;
$dbh->disconnect;

following is the db.sql file query 以下是db.sql文件查询

insert into branch_build_info(patch_name,branch_name,ftp_path)
values('$patch_name','$svn_url','$ftp_loc/$patch_name.tar.gz');

pls help me how can i pass the variable so that i can run more the one insert query at a time. 请帮助我如何传递变量,以便一次可以运行多个插入查询。

If you can influence the sql-file, you could use placeholders 如果可以影响sql文件,则可以使用占位符

insert into ... values(?, ?, ?);

and then supply the parameters at execution: 然后在执行时提供参数:

my $ftp_path = "$ftp_loc/$patch_name.tar.gz";
$sth->execute( $patch_name, $svn_url, $ftp_path) or die ...

That way you would only need to prepare the statement once and can execute it as often as you want. 这样,您只需要准备一次语句即可执行任意次数的语句。 But I'm not sure, where that sql-file comes from. 但我不确定该sql文件来自何处。 It would be helpful, if you could clarify the complete workflow. 如果您可以阐明完整的工作流程,将很有帮助。

there's a few problems there - looks like you need to read in all of the SQL file to make the query, but you're just reading it a line at a time and trying to run each line as a separate query. 那里有一些问题-看起来您需要读入所有SQL文件才能进行查询,但是您一次只能读取一行,并尝试将每一行作为单独的查询运行。 The <SQL> just takes the next line from the file. <SQL>只是从文件中提取下一行。

Assuming there is just one query in the file, something like the following may help: 假设文件中只有一个查询,则如下所示可能会有所帮助:

open( my $SQL_FH, '<', $sqlfile)
   or die "Can't open file '$sqlFile' for reading: $!"; 

my @sql_statement = <$SQL_FH>;

close $SQL_FH;   

my $sth = $dbh->prepare( join(' ',@sql_statement) )
   or die "Can't prepare @sql_statement: $!";

$sth->execute();

However, you still need to expand the variables in your SQL file. 但是,您仍然需要在SQL文件中展开变量。 If you can't replace them with placeholders as already suggested, then you'll need to either eval the file or parse it in some way... 如果您不能按照建议的那样用占位符替换它们,那么您将需要eval文件或以某种方式解析它...

You can use the String::Interpolate module to interpolate a string that's already been generated, like the string you're pulling out of a file: 您可以使用String :: Interpolate模块对已生成的字符串进行插值,例如要从文件中拉出的字符串:

while ($sqlStatement = <SQL>) 
{
   my $interpolated = new String::Interpolate { patch_name => \$patch_name, svn_url => \$svn_url, ftp_loc => \$ftp_loc };
   $interpolated->($sqlStatement);

   $sth = $dbh->prepare($interpolated) or die qq("Can't prepare $interpolated");

   $sth->execute() or die ("Can't execute $interpolated");
}

Alternately, you could manually perform replacements on the string to be interpolated: 或者,您可以手动对要插入的字符串执行替换:

while ($sqlStatement = <SQL>) 
{
   $sqlStatement =~ s/\$patch_name/$patch_name/g;
   $sqlStatement =~ s/\$svn_url/$svn_url/g;
   $sqlStatement =~ s/\$ftp_loc/$ftp_loc/g;

   $sth = $dbh->prepare($sqlStatement) or die qq("Can't prepare $sqlStatement");

   $sth->execute() or die ("Can't execute $sqlStatement");
}

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

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