in reply to Perl/DBI/Oracle and strings with spaces

My first suggestion is to always, always check the status of your DBI calls, like so:
my $sth=$dbh->prepare($MYSQL) or die "Could not prepare $MYSQL: $DBI:: +errstr\n"; ...etc. for all connects, executes, etc.
As far as passing spaces, that should not be a problem. When you execute the query, the DBI should quote it for you. Try adding the line
$dbh->trace(2);
at the top of your script (after $dbh is created, of course) to see some of the details of what is going on. You could also use the 'quote' function to see exactly what it is sending:
my $string = "find me"; my $newstring = $dbh->quote($string) or die "Could not quote $string: +$DBI::errstr\n"; print "($string) has become ($newstring)\n";
('quote' is mostly used to help escape characters like a single apotrophe, which has special meaning inside SQL.)

Finally, consider moving your SQL statements into a variable: it will read better, be easier to maintain, and eliminate double typing of the same string in multiple places:

my $bazfind = shift || "12"; print "Looking for $bazfind.\n"; my $dbh=DBI->connect($SID, $user, $pass, {AutoCommit=>1}) or die "Could not connect to $SID: $DBI::errstr\n"; my $FOOBAR_SQL = "SELECT FOO FROM BARVILLE WHERE BAZ=?"; my $sth=$dbh->prepare($FOOBAR_SQL) or die "Could not prepare $FOOBAR_SQL: $DBI::errstr\n"; $sth->execute($bazfind) or die qq!Could not execute $FOOBAR_SQL with "$bazfind": $DBI::errst +r\n!; ...etc. etc.
Note how the single declaration of $FOOBAR_SQL not only saves typing it in repeated times, but looks nicer too.

Replies are listed 'Best First'.
Re: Re: Perl/DBI/Oracle and strings with spaces
by runrig (Abbot) on Dec 09, 2000 at 20:40 UTC
    I would connect using:
    my $dbh=DBI->connect($SID, $user, $pass, {RaiseError=>1});
    and avoid typing all the '.. or die' clauses. Because if you get an error, it gives you a place to start looking and if you need to, you can just add a debug 'print' statement before the failing statement on the next go round.

    That being said, there should be no reason why your statement can not find fields with spaces in them. My guess is that either the prepare is failing or your bind variable has characters besides spaces that you're not expecting. Maybe you need to convert the bind_variable to hex and print that out to see if there are any weird characters like linefeeds sneaking in. One guy I worked with swore up and down that "placeholders don't work" and he avoided using them, and we found that the input was getting a carriage return character. (He also complained that "arrays don't work" for similar reasons, but that's another story - he would always avoid using things instead of debugging and figuring out what's wrong).

    Also, I hope that you don't really have html breaks in the middle of your sql statement because that definitely would cause the prepare to fail.