473,670 Members | 2,353 Online
Bytes | Software Development & Data Engineering Community
+ 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.P lease 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 6634
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
5396
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 = http://www.domain.com/cgi-bin/cart.pl?name=Book+Title&Stk=D123456&price=5.00 $co = new CGI; print $co->redirect($cartStr); print $co->start_html; $co->end_html;
4
6469
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 define the servername / hostname in my Perl Progrem.. Here is the code:
0
9738
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. I need it to be compiled with threads. Anyone have any wisdom on how best to do this? Here's a transcript of my latest attempt. It's long; you might want to skip to the bottom, where I try "make" and the fatal errors start happening.
4
3701
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 functionality of php. The extension uses the '$perl->eval' function. i am kind of stuck with the syntax or how to put the php variable into the perl script. I have a form where the user puts in a grid reference. Then a php script that gets the...
1
3296
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, if I click on the Update button, the values in the text-area must be updated in the mysql database. I am storing the values of the text-area in a variable. I am using javascript to pass the variable.
0
1391
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 my "Pools" table One from MS SQL Manager as a test, and another from my PERL script. The one inserted by MS SQL Manager shows up everytime. The row from my PERL script doesn't show up on MS SQL Manager and ONLY shows up on my PERL script and...
1
2275
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 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 my "Pools" table One from MS SQL Manager as a test, and another from my PERL script....
82
10016
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) html b) perl script (print and inserting into database) Cheers... Andrew
0
2104
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 program is printing the output in a nice form.However I want to pass a variable in the $t->cmd("SELECT * FROM TABLE_NAME WHERE INSTANCE_NAME LIKE '%hostname%' ;"); Here I want hostname to be replaced by a variable name like $t->cmd("SELECT * FROM...
0
8388
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8907
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8663
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7423
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5687
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4215
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2804
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 we have to send another system
2
2046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1799
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.