Date sent:              Tue, 03 Jul 2001 21:28:12 +0100
To:                     [EMAIL PROTECTED]
From:                   Ian Summers <[EMAIL PROTECTED]>
Subject:                Re: Bind Parameters with MS Access

Ian,
> 
> The real problem is that I'm trying to think of a way to speed up updating 
> an Access table from a csv file. I can't think of a way to use bind 
> parameters. The process I'm using is:
> 
> 1) Read first line of file which is the column names
> 2) Read each subsequent line from file
> 3) If the row already exists in the table:
>       UPDATE table SET col1='value1',col2='value2' WHERE ID=id
>     else
>       INSERT INTO table(col1,col2) VALUES('value1','value2')
> 
> Because each UPDATE/INSERT sql is unique there is no way bind parameters 
> can be used. Unless anyone knows of a way ...
> 

Concept:

1) Create two separate statement handle with placeholders, one for 
inserting, the other for updating.
2) Bind your columns to the needed data types (this is not always 
necessary, try first without binding, because data types may be 
guessed corectly using placeholders)
3) check if id is present in table, if yes, execute the updating 
statewment handle, if not, execute the inserting statement handle. If 
your id column is configured to be a unique primary key (what it 
should be), you can check for the existence of id by just always 
trying to insert first and update afterwards if you get a key 
violation error. Below there is some untested prototype code for all 
that:

<pseudo_perl_code>

### .... get your database handle with RaiseError on

my @columns = get_your_columns_as_an_array_from_somewhere();

### generate your sql statements with placeholders for updating and 
inserting, and prepare() the respective statement handles...

my $update_sql = "UPDATE table SET " . join(', ',  map {$_ . '= ?'} 
@columns) . " WHERE id = ?";
my $insert_sql = "INSERT INTO table (" . join(', ', @columns) . ") 
VALUES (" . join(', ', map {'?'} @columns) . ")";

my $update_sth = $dbh->prepare($update_sql);
my $insert_sth = $dbh->prepare($insert_sql);

### the follwing code binds the placeholders to your data types, but 
may not be necessary, try without first...

my @data_types = ('your', 'datatypes', 'here');
my $i = 1;

for (@columns) {
        
        $update_sth->bind_param($i, $columns[$i], $data_types[$i]);
        $insert_sth->bind_param($i, $columns[$i], $data_types[$i]);
}

### action starts here

open LOG, "> 'logfile'" or die $!;
while (my @values = some_action_to_get_your_values_as_an_array()) {
        
        ### try inserting first...
        eval {
        
            $insert_sth->execute(@values);
        };
        if ($@) {
                
                print LOG "INSERT failed on values:\n@values:\nError 
message:\n$@\n\n";

                ### and try updating on error...

                eval {
                
                        $update_$sth->execute(@values);
                };
                print LOG "UPDATE failed on values:\n@values:\nError 
message:\n$@\n\n" if $@;
        }
}

close LOG;
$dbh->disconnect;

</pseudo_perlcode>

There is much left to be done regarding transaction controlling, 
about the correct syntax of bind_param() (read perldoc DBI about 
this), and about how to get the correct data types for bind_param(), 
which can be also be fetched on the fly using the TYPE attribute of 
executed statement handles; but I think the basic idea of two 
separate statement handles should be clear.
    Again, this approach only works if the id column is a unique 
primary key. And, of course, there may be also other errors beyond 
key voilations, therefore check the error logfile (which also will 
contain information if incorrect data types were bound). If id it is 
not a primary unique key, you must check for the existence of id with 
a separate SELECT.
    BTW, if you really want to speed up your application and 
portability is not an issue, write an appropriate VBA macro within 
your Access database and trigger it from Perl.

HTH

Bodo
[EMAIL PROTECTED] 

Reply via email to