I think it will be nice if you write  more hgher level methods for each
InsertSelect, Update, Create, And Delete commends.

Ex:- An Insert method which take the table name and a Hash as args and
cunstruct the query and execute it

sub Insert($%){
    $self = shift;
    my ($table,%data) = @_;
    my @keyArray = keys %data;
    my @valueArray;
    foreach my $key (@keyArray){
        $valueArray[++$#valueArray] = $self->{dbh}->quote($hash{$key});
    }

    my $query = "INSERT INTO $table (" . join(',', @keyArray) . ") VALUES("
. join(',' @valueArray) . ")";
    $self->{dbh}->do($query);

}

like that you can also write a method for select
which takes the tablename, a list of fields to query and the where cluse,
and orderby clouse as arguments

And a update method and list goes on....
You may also write methods for locking and unlocking, commiting and
rollbacking ....

That approach will minimize the SQL that you have to write when you use
databases




LRMK
----- Original Message ----- 
From: "Rod Za" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, June 25, 2004 3:04 AM
Subject: Class to select/insert/delete/update


> Hello,
>
> i'm trying to make a class to do select/insert/delete/update on a MySQL
table (via DBI).
> this is a snip of code:
>
> sub query{
>         my $self = shift;
>         my($sql) = @_;
>         my @result;
>         my $sth = $self->{dbh}->prepare($sql) or return undef;
>         if($sql =~ /delete|insert/gi){
>                 $sth->execute() or return undef;
>                 my $rows = $sth->rows;
>                 ($rows == 0) ? "0E0" : $rows;
>         } else {
>                 $sth->execute() or return undef;
>                 my @row;
>                 while(@row = $sth->fetchrow_array){
>                         foreach my $i (0..$#row){
>                                 push(@result,$row[$i]);
>                         }
>                 }
>                 $sth->finish();
>                 return @result;
>         }
> }
>
> I like to receive an opnion or someone to indicate me something similars.
>
> Thnak you
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
>
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to