castaway has asked for the wisdom of the Perl Monks concerning the following question:
The problem:
I have a table (or actually two similar tables, but hopefully thats not relevant - see code), that looks like this:
The mud_id is unique, and the names should also be unique, at least, I check before adding..mud_id | Name | <Other fields .. > ------------------------------------------------ 1 FinalFrontier <IP, Port etc> 2 Timewarp 3 Timewarp-RENEGADE 4 Kylere'srealm 5 MG:test@blah
Code:
sub getMudName { # Get a name or all names matching or abbreviated by given name # Parameter: Object, Name my ($dbobj, $type, $name) = @_; my $dbh = $dbobj->{$databasehandle}; $dbobj->{$errormsg} = ''; my $table; if($type eq 'i3') { $table = 'Intermud3'; } elsif($type eq 'i2') { $table = 'Intermud2'; } else { $dbobj->{$errormsg} = "Oops, invalid type: $type\n"; return undef; } # Tyes fun version: select Name from intermud2 where Name = 'Timewarp' + or (0=(select count(*) from intermud2 where Name='Timewarp') and Nam +e like 'Timewarp%') # my $stmS = "SELECT Name FROM $table WHERE Name like '$name%' orde +r by "; my $stmS = "SELECT Name FROM $table WHERE Name like ? order by "; $stmS .= "length(Name)"; $dbobj->debug("getMudName: $stmS\n"); $stmS = $dbh->prepare($stmS); # my $res = $stmS->execute(); my $res = $stmS->execute($name . '%'); if(!$res) { $dbobj->{$errormsg} ="Can't select from $table: " . $dbh->errstr() . "\n"; return undef; } my $ids = $dbh->selectcol_arrayref($stmS); $dbobj->debug(Dumper($ids)); if(!$ids) { $dbobj->{$errormsg} = "No Name matching name: " . $stmS->err() . "\n"; return undef; } if(lc($ids->[0]) eq lc($name)) { return [$ids->[0]]; } return $ids; }
C.
20030225 Edit by Corion: Removed runaway bold tag
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: DBI, quoting and like - SQLite
by bart (Canon) on Feb 25, 2003 at 09:22 UTC | |
by castaway (Parson) on Feb 25, 2003 at 09:30 UTC | |
|
Re: DBI, quoting and like - SQLite
by jammin (Novice) on Feb 25, 2003 at 09:38 UTC | |
by castaway (Parson) on Feb 25, 2003 at 10:06 UTC | |
|
Re: DBI, quoting and like - SQLite
by diotalevi (Canon) on Feb 25, 2003 at 13:35 UTC | |
|
Re: DBI, quoting and like - SQLite
by p6steve (Sexton) on Feb 25, 2003 at 19:49 UTC | |
by castaway (Parson) on Feb 26, 2003 at 07:26 UTC | |
by dws (Chancellor) on Feb 26, 2003 at 08:12 UTC |