RE: Efficient select/insert



Is there a reason you need to make the round trip from the database,
through your program, and then back to the database? If not then just
do an insert select:

my $sql = "insert into table2 select null, col2, col3, col4 from table1
where col1 = ?";
$dbh->do($sql);

-----Original Message-----
From: Jonathan Mangin [mailto:jon.mangin@xxxxxxxxxxx]
Sent: Tuesday, April 26, 2005 4:14 PM
To: dbi-users@xxxxxxxx
Subject: Efficient select/insert


>I would like to select several rows from one table
> and insert them into another nearly identical table
> using Perl/DBI:
>
> my @array = $q->param(); # HTML checkboxes
>
> foreach my $element (@array) {
> my $sql = "select col2, col3, col4 from table1
> where col1 = ?";
> my $sth = $dbh->prepare($sql);
> $sth->execute($element) or die $sth->errstr();
>
> my @row = $sth->fetchrow_array;
>
> $sql = "insert table2 (col1, col2, col3, col4)
> values (NULL, ?, ?, ?)";
> $sth = $dbh->prepare($sql);
> $sth->execute($row[0], $row[1], $row[2]) or die $sth->errstr(); }
>
> Is this efficient db interaction, or is there a better way? This is
> 3.23 but can upgrade if necessary.
>
> Thanks,
> Jon
>
>
Further...
I thought I could use fetchrow_arrayref and push an array
of arrays. The DBI docs say:

"Note that the same array reference is returned for each fetch, so don't
store the reference and then use it after a later fetch."

Sounds like I can't use that. Now I see execute_for_fetch. Does this
sound like a job for execute_for_fetch?

--J

.



Relevant Pages

  • Re: List of lists - baffled
    ... are collections of field data from a database. ... Note that the same array reference is returned for each fetch, ... don't store the reference and then use it after a later fetch. ...
    (comp.lang.perl.misc)
  • Re: KirbyBase
    ... creating objects from the database records was much easier. ... Hal, I don't know if you have had a chance to take a look at the beta yet, but I basically tried to implement a uniform way to specify one-to-one links, one-to-many links, and calculated fields in the ... I suppose it would in effect be embedding an array where all the ... My first couple of attempts at adding more complexity to KirbyBase did not honor this concept. ...
    (comp.lang.ruby)
  • Re: KirbyBase
    ... I'm an Object Guy and Jamey is a Database Guy. ... That requires the table name and key field name to be specified ... I suppose it would in effect be embedding an array where all the ... that is) might compress by a factor of 10 or more. ...
    (comp.lang.ruby)
  • Re: using BLOB objects and ...
    ... while saving the array object to the database which is ok but while ... convert it back to object of 4 dimention boolean array which will be ... will let you store your 4 dimensional boolean array in it. ... You may have to modify my suggestion ...
    (comp.lang.java.programmer)
  • Re: using BLOB objects and ...
    ... - You could use Memo if you converted your boolean array into a text string. ... boolean array when you fetch it from the database. ... You might be able to put the whole array in an OLE Object, ...
    (comp.lang.java.programmer)