Re: sql select with array
by davorg (Chancellor) on Oct 22, 2001 at 13:32 UTC
|
When you interpolate an array in a double quoted
string, the elements of the array are separated by the
contents of the $" variable. The default value
for this variable is a space, so your SQL statement
becomes "select fornavn efternavn cpr adresse zip city
telefon mobil g_afsluttet bemyndigelse_til tjenestested
id pic from people..." which is invalid SQL.
To fix the problem, you can set $" to a comma,
but whenever you change the value of a Perl special
variable, you should localise the change like
this:
{
local $" = ',';
my $row = $dbh->selectall_arrayref("select @selector from people whe
+re (fornavn like '%$search%') or (efternavn like '%$search%') or (cpr
+ LIKE '%$search%')",\ %attr);
}
--
<http://www.dave.org.uk>
"The first rule of Perl club is you don't talk about
Perl club."
| [reply] [d/l] |
Re: sql select with array
by blakem (Monsignor) on Oct 22, 2001 at 13:31 UTC
|
When @selector is interpolated into your string, the elements are being separated by a space. Since SQL engines expect those fields to be separated by commas, your code is
bombing out. So, you need to add commas into your SQL to get it to work properly.
One "questionable" (but fun) way of doing this is by modifying the global $" variable, which is the separator used when interpolating arrays into strings. Its default value is a single space, which explains the above situation.
Setting it to ',' yields code like this: (warning, untested)
my $sql;
{
local $" = ','; # localize $" for use in the next statement
$sql = "select @selector from people where foo like '%bar%'"
}
my $row = $dbh->selectall_arrayref($sql) or die "SQL Error: $sql";
-Blake
| [reply] [d/l] [select] |
|
I'm interested to know why you think that changing
$" is a "questionable" solution. It seems to me
that it's situations like this that $" was
invented for.
--
<http://www.dave.org.uk>
"The first rule of Perl club is you don't talk about
Perl club."
| [reply] |
|
local $"=','; my $s="@list";
my $s=join(',',@list);
As i said though I use it in obfus, snippets and temporary debuging code all the time. But only cause I like to. :-)
Yves
--
You are not ready to use symrefs unless you already know why they are bad. -- tadmc (CLPM) | [reply] [d/l] |
|
I always hesitate to tell anonymonks to go tinkering with global vars..... Its the case of "a little information can be dangerous." Once you understand the full implications, I think it is a fine solution, though.
-Blake
| [reply] |
Re: sql select with array
by busunsl (Vicar) on Oct 22, 2001 at 13:26 UTC
|
my $row = $dbh->selectall_arrayref('select ' .
join(',', @selector) .
" from people where (fornavn like '%$search%') or (efternavn like '%$s
+earch%') or (cpr LIKE '%$search%')",\ %attr);
| [reply] [d/l] |
|
Or, if you're not stuck on using just $dbh->selectall_arrayref and your database supports placeholders, couldn' you do this?
$sth = $dbh->prepare("SELECT " . '? ' x $#selector . "FROM people WHER
+E (fornavn LIKE '%$search%') or (eternavn LIKE '%$search%') or (cpr L
+IKE '%$search%'", \%attr); #dunno if you can do the \%attr part...
$sth->execute(@selector);
And then you've got a statement handle to get your data from.. I don't know if it's more efficient, but it seems a bit more readable to me. | [reply] [d/l] [select] |
|
| [reply] [d/l] [select] |
Re: sql select with array
by jeroenes (Priest) on Oct 22, 2001 at 13:29 UTC
|
In general "@array" concatenates the items
in the array in to a string without any separator
(but see perlvar).
In the case of SQL, you'd probably like comma's
between your items (see join):
...._arrayref( "select ". join( ', ', @selector) .
" from people .... "
Jeroen
| [reply] [d/l] [select] |
Re: sql select with array
by Anonymous Monk on Oct 22, 2001 at 13:35 UTC
|
| [reply] |