in reply to Re: Updating Specific Fields in MS Access
in thread Updating Specific Fields in MS Access

Thank you for the help. Some misconceptions I had have been cleared up.

Here's my new script:

#! c:\perl\bin\perl.exe -w # # InsertDrivers.pl #Importations and Declarations use Win32::ODBC; use DBI; use strict; my ( $dbh, $sth, $vals, $driver, $servlist); my $DSN='Database'; my $field1='server'; my $field2='nic1_drv_version'; #Connect to database $dbh=DBI->connect("dbi:ODBC:$DSN", "admin", "") or die "$DBI::errstr\n +"; #Open file for reading open(INPUT,"file.txt") || die; while (<INPUT>) { #use comma delimiter ($servlist, $driver)=split(/,/); #statement, prepare, execute, close handle my $sqlstatement=("UPDATE Servers SET nic1_driver_version=$driver +WHERE '$field1 eq $servlist\n'"); $sth=$dbh->prepare($sqlstatement); $sth->execute || die; $sth->finish; } close (INPUT); $dbh->disconnect;
When run, the output is:

DBD::ODBC::st execute failed: MicrosoftODBC Microsoft Access Driver Syntax error in number in query expression '5.0.67.0'. (SQL-42000)(DBD: st_execute/SQLExecute err=-1) at C:\Perl\scripts\test\ne w.pl line 31, <INPUT> line 1.
Died at C:\Perl\scripts\test\new.pl line 31, <INPUT> line 1.

5.0.67.0 is the first $driver value in file.txt.

Regards,
jw

Replies are listed 'Best First'.
Re: Re: Re: Updating Specific Fields in MS Access
by Enlil (Parson) on Apr 17, 2003 at 21:59 UTC
    Your almost there but, there is a problem with your SQL statement (it is not valid). The statement should be something like:
    my $sqlstatement=("UPDATE servers SET nicl_driver_version = '$driver' +WHERE '$field1' = '$servlist'");
    So you need to put single quotes around $driver as it is not all numeric, nor the SQL keyword NULL, I have also changed you WHERE clause to reflect what I think you want it to do. One good resource for SQL syntax is w3schools

    you also don't appear to be using $field2 or $vals for anything, and don't need the use Win32::ODBC; line for anything as well (you are not using that module in your code at all).

    One last thing is that you might want to prepare your SQL statement outside your loop, and then execute it on the inside, so you don't have to prepare the same statement time and time again unnecessarily). So your code might look something like this(untested):

    update:added line to script because I neglected to initialize $field1 (so that line is now there), which would be the reason for the failure, p01p0t was having in Re: Re: Re: Re: Updating Specific Fields in MS Access

    -enlil

      Your response is appreciated.

      When I try to place the prepare before the loop, it complains about uninitialized values.
      I tried using placeholders, to no avail, so I kept the prepare in the loop. Now I have:

      #! C:\perl\bin\perl.exe -w use DBI; #use strict; my $DSN='Database'; my $driver; my $servlist; my $field='Server'; #connect to database my $dbh=DBI->connect("dbi:ODBC:$DSN", "admin", "") || die; #Open file for reading open(INPUT,"file.txt") || die; while (<INPUT>) { chomp; my ($servlist, $driver)=split(/,/); my $sqlstatement=("UPDATE Servers SET nic1_driver_version='$driver +' WHERE '$field' = '$servlist\n'"); my $sth=$dbh->prepare($sqlstatement); $sth->execute || die; } close (INPUT); $dbh->disconnect;
      Spits back:
      DBD::ODBC::st execute failed: MicrosoftODBC Microsoft Access Driver Too few parameters. Expected 1. (SQL-07002)(DBD: st_execute/SQLExecute err=-1) at C:\Perl\scripts\test\newer.pl line 22, <INPUT> line 1.
      Died at C:\Perl\scripts\test\newer.pl line 22, <INPUT> line 1.

      I am confused about the parameters error.
      Regards,
      jw

        In order to use placeholders, put a '?' whereever you will need to plug data into the statement. Your UPDATE statement then becomes:

        my $sth = $dbh->prepare("UPDATE Servers SET nic1_driver_version=? WHER +E Server=?");

        Then, pass the values to plug into those placeholders to execute like so:

        $sth->execute($driver,$servlist);

        The statement doesn't need to change from one execution of the loop to the next. Therefore, the prepare can be done outside the loop. The values change each iteration and get plugged into the statement at execute time inside the loop.

        90% of every Perl application is already written.
        dragonchild