in reply to inserting and showing data from an MS Access database

Have a look in the Tutorials section under databases, and also the database section in Categorized Questions and Answers

You'll want the DBI module, and probably DBD::ODBC

And yes, you use the same syntax for any db query, regardless of whether it is an insert, select, update, or whatever.

Cheers,
Darren :)

  • Comment on Re: inserting and showing data from an MS Access database

Replies are listed 'Best First'.
Re^2: inserting and showing data from an MS Access database
by Anonymous Monk on Apr 11, 2006 at 07:12 UTC
    Hi Darren, thanks for the response. I know it would be the same syntax, but what about something like this:
    my $dbh = DBI->connect('DBI:ODBC:test1.mdb') my $sth = $dbh->prepare("SELECT something FROM table") my $sth = $dbh->prepare("INSERT INTO table VALUES (value1, value2, val +ue3)") # rest of program down here
    How would I handle the second my $sth? Can I use something other than sth?
      Can I use something other than sth?
      Yes, of course you can. $sth is just a variable name that's generally used in DBI examples. You can use anything you like :)

      Just a couple of other quick pointers that may save you some grief further down the track:

      • try to get into the habit of always checking the return values of your DBI calls. The general way to do this is:
        my $dbh = DBI->connect("DBI::ODBC ... etc $dsn") or die("Couldn't connect to $dsn: $DBI::errstr\n");
      • another good habit to get into is to use placeholders wherever you can. This a safer method and ensures that your strings will always be properly quoted. For example, instead of:
        my $sth = $dbh->prepare("INSERT INTO table VALUES (value1, value2, val +ue3)");
        You would do:
        my $sth = $dbh->prepare("INSERT INTO table VALUES (?, ?, ?)");
        and then later...
        $sth->execute($foo, $bar, $baz);
      You'll find plenty of examples of this and lots more information in the links I gave you earlier.

      Cheers,
      Darren :)

      yes, you can use something other than 'sth' .. that's just the common default/example variable name for a STatement Handle ..
      my $sth_ins = $dbh->prepare("INSERT ...."); my $sth_sel = $dbh->prepare("SELECT ...."); ... $sth_ins->execute(); ... $sth_sel->execute(); while( my $row = $sth_sel->fetchrow_arrayref() ){ ... }
        Thanks David, I'll give that a shot and see how it goes. I figured it would be something simple. I'll be glad when I'm comfortable enough with perl to deal with stuff like this without asking.
        Thanks for the replies, gentlemen. Now I've pretty much mangled the code but it almost works. The only problem is that the insert statement is inserting a blank row each time with no data in each column. The insert code i'm using is:
        my $sth_ins = $dbh->prepare("INSERT INTO users (firstname, lastname, s +tate, country) VALUES (?, ?, ? ,?)") or die "Couldn't insert the data +"; $sth_ins->execute($FORM{firstname}, $FORM{lastname}, $FORM{state}, $FO +RM{country});
        I was talking about this problem with a friend of mine who is vaguely familiar with Perl and he mentioned that $FORM{value} was the only way to retrieve the data from
        read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $FORM{$name} = $value; }
        Is there another way to do it or am I just overlooking something the way it is now?