473,471 Members | 4,095 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Perl Database SQL query variable passing

4 New Member
Hi,
I have the following perl script working for me.I am accesing the database from my perl script using use Net::Telnet(); package.
I am not using DBI package.as I stated earlier the following program is printing the output in a nice form.However I want to pass
a variable in the
Expand|Select|Wrap|Line Numbers
  1.  $t->cmd("SELECT * FROM TABLE_NAME WHERE INSTANCE_NAME LIKE '%hostname%' ;");
Here I want hostname to be replaced by a variable name like
Expand|Select|Wrap|Line Numbers
  1. $t->cmd("SELECT * FROM TABLE_NAME WHERE INSTANCE_NAME LIKE '%VARIABLE_NAME%' ;");.
I want just pass a variable in the above sql query so that I can change the VARIABLE_NAME.Please help me out with it.
Thank You
Vivek


Expand|Select|Wrap|Line Numbers
  1. $IPAddress = " ";
  2. $Login = " ";
  3. $password = " ";
  4. $Node = "hostname";
  5.  
  6. use Net::Telnet();
  7. $t= new Net::Telnet (Timeout => 3000 , Prompt => '/[%#\$>?:] $/' );
  8. $t->open("$IPAddress");
  9. print "\nConected!";
  10. $t->waitfor('/login: $/i');
  11. $t->print($Login);
  12. print "\nEntered the Username\n";
  13. $t->waitfor('/assword: $/i');
  14. $t->print($Password);
  15. print "\nEntered the Password!\n";
  16. @output=$t->cmd("export ORACLE_SID=$Node\n");
  17. print @output;
  18. print "\n Done with logging into the database\n";
  19. @output= $t->cmd("bash\n");
  20. print FILE @output;
  21. print @output;
  22. @output = $t->cmd("sqlplus username/pass\n");
  23. print @output;
  24. @output = $t->cmd("conn cusername/pass\n");
  25. print @output;
  26. @output = $t->cmd("SELECT * FROM TABLE_NAME WHERE INSTANCE_NAME LIKE '%hostname%' AND PARAM_KEY_NAME LIKE '%host';");
  27. print FILE @output;
  28. @output =  $t->cmd("exit\n");
  29. print @output;
  30. @output =  $t->cmd("exit\n");
  31. print @output;
Jan 27 '09 #1
4 6618
numberwhun
3,509 Recognized Expert Moderator Specialist
@sonu2die4

Why are you using % to specify a variable name? In Perl, a $ is for a scalar variable and a % is for a hash. If you have a $variable that you want to use, then use it in place of %hostname% as that is not correct. This is not dos scripting or any other like it.

So, you can write your statement like so:

Expand|Select|Wrap|Line Numbers
  1. @output = $t->cmd("SELECT * FROM TABLE_NAME WHERE INSTANCE_NAME LIKE '$hostname' AND PARAM_KEY_NAME LIKE '$host';");
  2.  
Also, please take note of the following:

1. Code tags are required around ANY code you post in the forums.
2. Any personal or private information (ie: usernames, passwords, IP addresses, url's, email addresses) are strictly forbidden in the general technical forums. This is for your safety and security and we expect you to please abide by this.

Regards,

Jeff
Jan 27 '09 #2
eWish
971 Recognized Expert Contributor
The % is a wildcard for MySQL. It can be used like he is using it.

--Kevin
Jan 27 '09 #3
numberwhun
3,509 Recognized Expert Moderator Specialist
@eWish
Ok, but if he is trying to pass a variable from a Perl script, he should pass the variable itself, not using the syntax he is using. I have seen plenty of code where I work (and written some) to use a variable in place of a parameter in the SQL line of the code, but have never used the % in the Perl code.
Jan 28 '09 #4
eWish
971 Recognized Expert Contributor
Here is some code I wrote a while ago to illustrate how to use a placeholder in the LIKE clause, which could be practiced here as well.
Expand|Select|Wrap|Line Numbers
  1. my $var = 'Car';
  2. my $like_var = '%' . $var . '%';
  3.  
  4. my $test = $dbh->prepare(qq|SELECT name FROM categories WHERE name LIKE ?|);
  5.    $test->execute($like_var);
  6.  
  7.    while (my @rows = $test->fetchrow_array()){
  8.        foreach (@rows) {
  9.           print $_, '<br>' ;
  10.  
  11.        }
  12.    }
  13. $test->finish();
  14.  
  15. my $regex_test = qq|^Car|;
  16.  
  17. my $another_test = $dbh->prepare(qq|SELECT name FROM categories WHERE name REGEXP ?|);
  18.    $another_test->execute($regex_test);
  19.  
  20.       while (my @rows = $another_test->fetchrow_array()){
  21.        foreach (@rows) {
  22.           print $_, '<br>';
  23.        }
  24.    }
  25. $another_test->finish(); 
This way you don't lose the % wildcard and you make safer and don't have to worry about escaping the data. Helps reduce the sql injections.

--Kevin
Jan 28 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Wet Basement | last post by:
I am passing data to a putcart.pl , parsing it, then building the query string to put the item in the shopping cart: $cartStr =...
4
by: banz | last post by:
Hello I have a problem to resolve: I wrote a Perlscript which caches data from a server (local on my machine) I would like to have a other connection to a remote server but I don't know how to...
0
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile....
4
by: billb | last post by:
I installed a perl extension for PHP to use some perl inside my php primarily because I have perl working with oracle and not php and oracle. So I want to use my old perl scripts, and use the...
1
by: satish2112 | last post by:
Hi, I have a text-area which contains values from mysql database and 2 buttons, Edit and Update. When I click on the Edit button, I can edit the text-area (initially non-editable). After this,...
0
by: Rebles | last post by:
I'm writing a PERL script to access and insert rows into a Microsoft SQL. i'm using MS SQL Server Management Studio Express (2005) to architect tables and queries. I've inserted two records into...
1
by: Rebles | last post by:
Hi, I just posted this in the MS SQL Section, but maybe my problem is rooted in Perl, so it's more appropriate to post here instead (sorry for the double post) I'm writing a PERL script to...
82
by: happyse27 | last post by:
Hi All, I modified the user registration script, but not sure how to make it check for each variable in terms of preventing junk registration and invalid characters? Two codes below : a)...
0
by: sonu2die4 | last post by:
Hi, I have the following perl script working for me.I am accesing the database from my perl script using use Net::Telnet(); package. I am not using DBI package.as I stataed earlier the following...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.