List:General Discussion« Previous MessageNext Message »
From:Paul DuBois Date:July 28 1999 2:24am
Subject:Re: Sample perl script
View as plain text  
At 9:04 PM -0500 7/27/99, Thimble Smith wrote:
>At 21:52, 19990727, Rich Bowen wrote:
>>Richard Reina wrote:
>>>
>>> Can someone send me a very simple  perl script that connects to a mysql
>>> database and modifies a table.
>>
>>use DBI;
>>use strict;
>>my $dbh = DBI->connect('DBI:mysql:database', 'username',
>>			'password');
>
>die "connect failed: $DBI::errstr" unless $dbh;
>
>>my $sth = $dbh->prepare("insert into tablename
>>			(fname,lname,email)
>>			values
>>			('Rich','Bowen','junkmail@stripped')
>>			");
>
>die "prepare failed:", $dbh->errstr unless $sth;
>
>>$sth->execute;
>
>$sth->execute or die "execute failed:", $sth->errstr;
>
>$dbh->disconnect;
>
>>That's about as simple as it gets.
>
>That was too simple.  Also, if you're storing character data
>(CHAR, VARCHAR, BLOB, etc.), don't forget to quote your values
>before you insert them!  Or, use the placeholder feature of DBI
>which will quote them for you.


Or you could avoid all the error checking by telling DBI to print
a message and die automatically if an error occurs:

my $dbh = DBI->connect('DBI:mysql:database', 'username',
			'password', { RaiseError => 1 });

--
Paul DuBois, paul@stripped
Thread
Sample perl scriptRichard Reina28 Jul
  • Re: Sample perl scriptRich Bowen28 Jul
    • Re: Sample perl scriptThimble Smith28 Jul
      • Re: Sample perl scriptPaul DuBois28 Jul
  • Installing on NTKevin Anderson28 Jul
    • Installing on NTJani Tolonen29 Jul
  • Re: Installing on NTChristian Mack28 Jul
Re: Sample perl scriptTom Cunningham28 Jul