Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a form to input some user information that I need to insert and redisplay from an Access database. So far, in every tutorial I've seen about reading data from a database, my $sth = $dbh->prepare("sql statements") and I understand that for retrieving data, but what about inserting data then retrieving? I don't think I would be able to use two my $sth = $dbh->prepare("sql statements"), one to insert and one to retrieve. I'm pretty new to perl but am very interested in learning. If this is an easy problem and you'd rather not just give me the answer, a point in the right direction would help greatly. I know that I have alot to learn about Perl, but it seems that this shouldn't be a major problem, I just can't seem to figure out the proper way of making it work. Any help is appreciated.

David

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

Replies are listed 'Best First'.
Re: inserting and showing data from an MS Access database
by McDarren (Abbot) on Apr 11, 2006 at 06:05 UTC
    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 :)

      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() ){ ... }
Re: inserting and showing data from an MS Access database
by gube (Parson) on Apr 11, 2006 at 06:14 UTC