MakerDB.pm

package MakerDB; use strict; use warnings; use Carp; use DBI; use Data::Dumper; my $dbh; my $sth; my $name, my $pass; use Class::MethodMaker get_set => [ { -read_cb => sub { my $self = shift; my $dsn = 'DBI:mysql:mydb'; my $dbuser ='root'; my $dbpass = ''; eval { $dbh=DBI->connect($dsn, $dbuser, $dbpass, { RaiseError => 1, AutoCommit => 0 } ); }; croak "\n\ncould not connect : $@" if $@; return $dbh; } },'connect', { -read_cb => sub { print "Enter Name : "; chomp($name = <STDIN>); print "Enter Password : "; chomp($pass = <STDIN>); my $self = shift; my $query = qq{INSERT INTO userlog VALUES(?,?)}; eval { $sth = $dbh->prepare($query); $sth->execute($name,$pass); }; croak "\n\nCheck the Query..Data cannot be inserted : +$@" if $@; return $sth; } },'save', { -read_cb => sub { my $self = shift; print "Enter the Name of the Record to be Deleted :\t" +; chomp($name = <STDIN>); my $query = qq[DELETE FROM userlog WHERE NAME = '$name']; eval { $sth = $dbh->do($query); }; croak "\n\nDelete Query Failed : $@" if $@; return $sth; } },'delete' ], new => 'new'; 1;
This will be your .pl file ...
use package MakerDB; use strict; my $newObj; my $dbh; my $sth; $newObj = MakerDB->new(); print "\n\tSuccessfully connected to DB\n\n" if $dbh=$newObj->conne +ct(); # Insert a record print "\nData Insertion Sucessful\n" if $newObj->save(); $sth=$dbh->prepare("SELECT NAME, PASSWORD FROM userlog"); $sth->execute(); my $href; while ($href=$sth->fetchrow_hashref()) { print "\n"; print Dumper $href; } # Delete a record $newObj->delete();

In reply to Writing DB Module using MethodMaker - Some More methods Added.. by vishi83

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.