Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Database copy table with DBI

by poj (Abbot)
on Nov 14, 2016 at 08:13 UTC ( [id://1175856]=note: print w/replies, xml ) Need Help??


in reply to Database copy table with DBI

Try changing this line to use method fetchrow_arrayref

while (my $insert = $table_results->fetchrow_arrayref()) {
poj

Replies are listed 'Best First'.
Re^2: Database copy table with DBI
by Random_Walk (Prior) on Nov 15, 2016 at 06:54 UTC

    One quick question. As the data needs to be flattened to an array to insert, is there (in the general case) any benefit in using fetchrow_arrayref? I am doing a similar task to the OP, copying entire DB tables. I have used fetchrow_array, then insert the array using a prepared insert. I would benchmark the two, but in my case the fetch is from an well known online service desk tool providing instant gratification ;) and the variance in performance between runs is large enough to mask any difference between fetching @ or \@.

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
Re^2: Database copy table with DBI
by cbtshare (Monk) on Nov 14, 2016 at 21:38 UTC
    Thank you, I will try this.using $table_results->fetchrow_arrayref() would seem to make sense, since I will then insert @$insert which is an array ref itself as the data. But if I wanted to use $table_results->fetchrow_array(), what then would I use within $sth_insert->execute( ); ? Thank you

      Why not print and insert in the same loop ?

      while( my @results = $table_results->fetchrow_array()){ print join ",",@results; print "\n"; $sth_insert->execute(@results); }
      poj
        thank you, I am however getting an error when using your method it always complains about the join statement

        Use of uninitialized value $results14 in join or string at rdscopy.pl line 92. and I also get

        DBD::mysql::st execute failed: Duplicate entry '10157236320140648_1015 +7237405105648' for key 'PRIMARY' at rdscopy.pl line 94. DBD::mysql::st execute failed: Duplicate entry '10157236320140648_1015 +7237405105648' for key 'PRIMARY' at rdscopy.pl line 94.

        ##print and insert at the same time while( my @results = $table_results->fetchrow_array()) { print join(",",@results); print "\n"; $sth_insert->execute(@results); }

        But I do not get that duplicate error when I use the code below, do you know why? Its the same data

        while (my $insert = $table_results->fetchrow_array()) { $sth_insert->execute(@$insert); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1175856]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-19 10:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found