Reading from ms Access

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • new2perl
    New Member
    • Dec 2008
    • 1

    #1

    Reading from ms Access

    I am new to using perl and am trying to read data from an ms access database.
    I have opened the database and can extract data from a table with limited success.
    What I have done so far is open the database using
    use Win32::ODBC;
    etc

    Then I have the following code:
    Code:
    #while ($db->FetchRow())  { 
      while ($i<10) {
      $db->FetchRow() ;
        my %Data = $db->DataHash();
        print $i," ",$Data{Au_plot},"," ;
        print $Data{Cu},"," ;
        print $Data{Zn},"," ;
        print $Data{Pb};
      $i++;
      print "\n";
    }
    This works ok as the first 10 rows are extracted from the table I have selected.
    But when I use
    Code:
    while ($db->FetchRow())  { 
    
    instead of
    while ($i<10) {
    the first record extracted is number 2 in the table and from then on only every second record is extracted form the table.

    Could someone tell me what is going on please?
    Thanks
    Chris
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by new2perl
    I am new to using perl and am trying to read data from an ms access database.
    I have opened the database and can extract data from a table with limited success.
    What I have done so far is open the database using
    use Win32::ODBC;
    etc

    Then I have the following code:
    Code:
    #while ($db->FetchRow())  { 
      while ($i<10) {
      $db->FetchRow() ;
        my %Data = $db->DataHash();
        print $i," ",$Data{Au_plot},"," ;
        print $Data{Cu},"," ;
        print $Data{Zn},"," ;
        print $Data{Pb};
      $i++;
      print "\n";
    }
    This works ok as the first 10 rows are extracted from the table I have selected.
    But when I use
    Code:
    while ($db->FetchRow())  { 
    
    instead of
    while ($i<10) {
    the first record extracted is number 2 in the table and from then on only every second record is extracted form the table.

    Could someone tell me what is going on please?
    Thanks
    Chris
    First, please use code tags around any code you provide in the forums. They are required and need to be added if you forget them.

    As far as your questions goes, I don't use MS Access. I like to stick to MySQL myself, but in Perl, when you access a database, you typically use the DBI module in conjunction with a DBD (database driver module). For instance, you always "use DBI", but if I am using MySQL, I would also include "use DBD::mysql" as well so that DBI knew how to interact with my database of choice.

    In your case, its MS Access and unfortunately I don't think there is specifically a driver for it, but I know that I have read about people using DBD::ODBC for the interaction. You could give it a try and see if it works for you. I know MS Access is mentioned on the page.

    In addition, you may want to search Google as I am sure they have a plethora of information on the topic of using Perl with MS Access.

    Regards,

    Jeff

    Comment

    Working...