简体   繁体   English

在perl中执行sqlplus命令

[英]Execute sqlplus command in perl

I am trying to check sql connection via sqlplus command in perl.我正在尝试通过 perl 中的 sqlplus 命令检查 sql 连接。 I have used the following code to do this.我使用以下代码来做到这一点。

print "Befores\n";
$rc=system("sqlplus system/system @sqlfile.sql");
print "After $rc\n";
sleep(10);

The returnes value in rc is 0 this time.这次rc中的返回值为0。 But when I give wrong credentials like.但是当我给出错误的凭据时。 The sqlfile.sql file contains the only sql command 'EXIT'. sqlfile.sql 文件包含唯一的 sql 命令“EXIT”。

$rc=system("sqlplus systemabc/system @sqlfile.sql");

This also ends with the return code 0. But, manually doing the Credentials are wrong.这也以返回代码 0 结束。但是,手动执行凭据是错误的。 Help me out in solving this...帮我解决这个问题...

Thanks in advance.提前致谢。 :) :)

Yes dear friens,,, It's working fine now.是的,亲爱的朋友,,,现在工作正常。

I just added '\\' before @sqlfile.sql, ie, I escaped the '@' symbol and I mentioned the database name after my password.我只是在@sqlfile.sql 之前添加了'\\',即,我对'@' 符号进行了转义,并在密码后提到了数据库名称。 Now I am getting the perfect output.现在我得到了完美的输出。 :) :)

The change is as bellow:变化如下:

$rc=system("sqlplus -L system/system@tstdb1 \@sqlfile.sql");

Here the format I used is: sqlplus -L Uname/Password@DBName \\@SqlFileContainingMySQLQuries这里我使用的格式是: sqlplus -L Uname/Password@DBName \\@SqlFileContainingMySQLQuries

And -L Specifies not to reprompt for username or password if the initial connection does not succeed.并且 -L 指定如果初始连接不成功,则不重新提示输入用户名或密码。 For more info goto About SQLPLUS有关更多信息,请转到关于 SQLPLUS

I also tried without escaping the '@' symbol before sqlfile.sql, But the change also should apply for inverted comas, I used Single inverted comas instead of double inverted comas as bellow as mentioned by @René Nyffenegger :我也尝试在 sqlfile.sql 之前不转义“@”符号,但更改也应适用于倒置昏迷,我使用了单倒置昏迷而不是双倒置昏迷,如下面@René Nyffenegger 所述:

$rc=system('sqlplus -L system/system@tstdb1 @sqlfile.sql');

Here the format I used is: sqlplus Uname/Password@DBName @SqlFileContainingMySQLQuries这里我使用的格式是: sqlplus Uname/Password@DBName @SqlFileContainingMySQLQuries

This also works fine.这也很好用。

Yeah... :) I too tried this, Thanks for the answer @user252025是的... :) 我也试过这个,谢谢你的回答@user252025

I was doing:我在做:

$rc=system("sqlplus -L user/user \@sql_file.sql");

Da rtrn code was wrong for me Now I ve changed to Da rtrn 代码对我来说是错误的 现在我已更改为

$rc=system("sqlplus -L user/user@Mydb \@sql_file.sql");

Its working fine.. Thanks again...它的工作正常......再次感谢......

The problem is the line问题是线路

$rc=system("sqlplus system/system @sqlfile.sql");

You have a string quoted with double quotes ( "..." ).您有一个用双引号 ( "..." ) 引用的字符串。 Such strings try to epxand variables (starting with @ , $ and % ).此类字符串尝试对变量进行扩展(以@$%开头)。 Since you have @sqlfile in the string, perl assumes this to be an array (which, of course it is not).由于字符串中有@sqlfile ,perl 假定这是一个数组(当然不是)。

Remedy:补救:

$rc=system('sqlplus system/system @sqlfile.sql');

Why are you doing this?你为什么做这个? DBI exists to provivide a sane and secure way of using databases and sensibly getting the output/return/errors from them. DBI 的存在是为了提供一种使用数据库和明智地从它们获取输出/返回/错误的理智和安全的方式。

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

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