If you can update the prospect object's methods, I would recommend adding a method that will add the prospect to the database for you. Another solution would be a method that returns all of the data for you in an array or hash. Barring that, the following really ugly hack could work:
{ # Get the new prospect my $prospect = shift; # Connect to the database my $dbh = DBI->connect('DBI:mysql:menagerie', 'menagerie') or die "Couldn't connect to database:" . DBI->errstr; # Set all of the variables that we need to pass to MySQL my @data = qw/ name address address2 state c +ity phone phone2 phone3 phone4 phoneType phone2Type phone3Type phone4Type email url interestLevel designFirm hostingFirm companyName /; my @insert_data; my $place_holders = '?,' x @data; chop $place_holders; # remove trailing comma foreach ( @data ) { no strict; my $method = $data[$_]; push @insert_data, $prospect->$method; } # Insert the new prospect in the database my $sth = $dbh->prepare("INSERT INTO prospect VALUES ( $place_ +holders )" ); # Execute the statement $sth->execute( @insert_data ) or die print DBI->errstr; # Lets get the prospect id that we just added back my $ID = $dbh->{'mysql_insertid'}; return $ID; }
The problem, of course, is when you turn off strict for the method calls, you run the risk of unintentionally calling the wrong method if you misspelled it. Further, if you have inherited the misspelled method, you could be in big trouble. The solution would be to write an autoload routine that detects whether or not the method exists and croaks rather than allowing you to inherit something. Of course, if you can write the autoload method, you could write the other methods :)
If you can't change the prospect methods, subclass it and add your own method. Much safer that way!
Other comments: you don't have a password in your connect string. If you left it off because you don't want us to see it, good for you! If you left it off because there is no password, shame on you! :)
I also noticed that you have repetitive phone data. As soon as someone has a fifth phone, your schema breaks. If you need to have multiple phones, create a separate table for them.
Cheers,
Ovid
Vote for paco!
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.
In reply to (Ovid) Re: MySQL / DBI / OOP
by Ovid
in thread MySQL / DBI / OOP
by rogueFalcon
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |