Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

variables inside SQL statements

by Anonymous Monk
on Oct 10, 2000 at 23:30 UTC ( [id://36102]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have some SQL that I want to run within/outside (don't care which one) a Perl script. I can connect to svrmgrl and log in fine, but I need to use Perl variables (for paths) within the SQL statements.

Replies are listed 'Best First'.
Re: variables inside SQL statements
by indigo (Scribe) on Oct 11, 2000 at 02:24 UTC
    Note - Interpolating variables into SQL is bad. Instead, use 'bind' variables, like so:
    my $sql = 'INSERT INTO table ( path ) VALUES (:1)'; # Compile sql my $sth = $dbh->prepare($sql) or die $dbh->errstr; # Bind path to :1 $sth->execute('/path/to/this') or die $dbh->errstr;
        You mean there are databases other than mySQL and Oracle? I don't understand. :)
Re: variables inside SQL statements
by dragonchild (Archbishop) on Mar 28, 2004 at 17:05 UTC
    Go to the DBI docs and look up placeholders.
Re: variables inside SQL statements
by Anonymous Monk on Mar 28, 2004 at 16:53 UTC
    nothing wrong with the previous answers for your precise need. If it weren't paths, perhaps a string like "Where's the beef O'Reilly?" you can also use:

    $dbh->quote( "Where's the beef O'Reilly?" );

    Originally posted as a Categorized Answer.

Re: variables inside SQL statements
by AgentM (Curate) on Oct 11, 2000 at 00:23 UTC
    huh? you can probably connect from within your perl script to the SQL database using <a href="http://www.unixodbc.com>unixODBC or the more popular albeit older alternative DBI. DBI provides question mark semantics that provide exactly the solution you are looking for...

    Originally posted as a Categorized Answer.

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://36102]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-19 21:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found