in reply to Re: SQL LIKE and Binding Variables
in thread SQL LIKE and Binding Variables
Thanks. I thought blackslash was the right way to escape "%" in a SQL statement also, but I couldn't seem to get the syntax right.
I tried your recommendation, but all I managed to get was a different error: "Modification of a read-only value attempted at ..." and it gives me a reference to the line with the my @values = map { s/([_%])/\\$1/g; "%$_%" } bit. Maybe I didn't implement it quite right?
Here's what my test code looks like at the moment:
#!/usr/bin/perl use warnings; use DBI; my $db = DBI->connect("dbi:SQLite:music.sqlite","",""); my @values = map { s/([_%])/\\$1/g; "%$_%" } ( 'pearl', 'temple'); my $like_clause = join( " or ", map { "Songs.Artist LIKE ?" } @values) +; my $sth = $db->prepare( "SELECT * FROM Songs where ($like_clause)" ); my $all = $sth->execute( @values ) or die("Failed to Execute SQL"); foreach my $row (@$all) { my ($id, $Track, $Title, $Artist, $Album, $Year, $Genre, $Path) = @$ro +w; print "\nid: $id\nTrack: $Track\nTitle: $Title\nArtist: $Artist\nAlbum +: $Album\nYear: $Year\nGenre: $Genre\nPath: $Path\n";
Any other thoughts?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: SQL LIKE and Binding Variables
by Anonymous Monk on Nov 03, 2009 at 23:07 UTC |