Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

I got confuse with INSERT

by padawan_linuxero (Scribe)
on Mar 14, 2008 at 23:25 UTC ( #674313=perlquestion: print w/replies, xml ) Need Help??

padawan_linuxero has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks
Yesterday I post a question regarding a problem in my code when I try to do an Insert This is the link to last post
The help was good but I still can't understand it, and well did not run as well.
The original code was like this :
my $dbhX1 = DBI->connect('dbi:XBase(RaiseError=1):'); my $select1 = $dbhX1->prepare("SELECT * FROM r501"); $select1->execute(); print "\nEjecutar SELECT de r501\n"; my $mysql_dbh1 = DBI->connect("DBI:mysql:database=$datafilename;ho +st=localhost", "root", "xyz123", {'RaiseError' => 1}); while ( (my @row) = $select1 -> fetchrow_array() ){ my $sthsql = $mysql_dbh1->prepare ("INSERT INTO r501 (reg, pat, ped, + adnasec, impexp, cveped, adnaent, crupimp, rfcimp, curpaa) VALUES ($row[0], $row[1], $row[2] +, $row[3], $row[4], $row[5], $row[6], $row[7], $row[8], $row[9]") $sthsql->execute(); }
I trim the amount of the data being inserted but still a viable example. I hope someone can explain it a little bit more with an example, sorry if these is to much work, I really like perl has help me do things that would take a lot of time doing in something else, it just that I did not get it . Thank you !

Replies are listed 'Best First'.
Re: I got confuse with INSERT
by grep (Monsignor) on Mar 14, 2008 at 23:59 UTC
    Here's (kind of) what I would do. Everything I do is explained in more detail in the DBI docs. Definitely read the section on placeholders. Not only does it handle quoting for you, it makes this easier by predefining an array with your column names.

    I'm guessing the column names in the 1st DB are the same as the second DB.

    ## UNTESTED ## Setup the column names - This way when you need to change it, you o +nly change in one place ## The column names will also stay in order my @col_names = qw/reg pat ped adnasec impexp cveped adnaent cr +upimp rfcimp curpaa/; my $col_names_string = join(',',@col_names); my $dbhX1 = DBI->connect('dbi:XBase(RaiseError=1):'); my $select1 = $dbhX1->prepare("SELECT $col_names_string FROM r501"); $select1->execute(); ## Now we create the string with placholders. my $placeholders = join(',',map { '?' } @col_names ); my $sql = "INSERT INTO r501 ( $col_names_string ) VALUES ( $placehold +ers )"; ## We are going to call this quite a bit so lets prepare it out side o +f the loop my $sth = $mysql_dbh1->prepare( $sql ); while ( my @row = $select1->fetchrow_array() ){ ## Stick the result in the other database $sth->execute(@row); }
    grep
    One dead unjugged rabbit fish later...
      Thank you for the example, but is there a faster way to do this? this is my other problem I need to read these tables in DBF to be able to provide to the user with real time information .
      But every time I run it it takes like 10 to 15 minutes in runnning
        There is a not a (practical) programmatic way to speed up that code (without knowing more). But I think you are heading down to XY_problem Territory.

        Can you give a clearer spec of what you are really trying to accomplish?
        Because, I can almost guarantee that copying a set of data from one table to another is not the solution.

        grep
        One dead unjugged rabbit fish later...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2021-09-27 17:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?