Re: Perl arrays into MySQL
A perhaps more perlish way would be, my $table = MyTable; my $sql = join ',', map {$_=?} keys % - Original Message - From: Daniel Kasak [EMAIL PROTECTED] To: Nik Belajcic [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Tuesday, June 01, 2004 5:15 AM Subject: Re: Perl arrays into MySQL Nik Belajcic wrote: This may be a silly question, but I am wondering if there is something opposite to: @row = $sth-fetchrow_array In other words, instead of fetching rows from MySQL and loading them into an array that can be accessed from Perl, I want to do the opposite - take a Perl (associative) array and load it into MySQL table. I know I could dump it into a text file and read it from there, but assuming I do not want to go through this intermediary step, how could I do it? Any suggestions appreciated. No there is not, but I urge to to implement it :) You can do something like: my %record; # Your record in memory you want in the table. It obviously needs to be filled my $sql_1; my $sql_2; my $sql_3; my $tablename = MyTable; # TODO: Set this another way? foreach my $key (keys %record) { $sql_1 .= $key, ; $sql_2 .= ?, ; $sql_3 .= %record{$key} . , ; } # Chop off the trailing comma and space $sql_1 = substr($sql_1, 0, length($sql_1)-2); $sql_2 = substr($sql_2, 0, length($sql_2)-2); $sql_3 = substr($sql_3, 0, length($sql_3)-2); my $sth = $dbh-prepare(insert into $tablename ( $sql_1 ) values ( $sql_2 )); $sth-execute($sql_3); CAUTIONARY NOTE!! I'm just learning Perl myself. The above code is NOT tested. But you should be able to get the idea anyway :) -- Daniel Kasak IT Developer NUS Consulting Group Level 5, 77 Pacific Highway North Sydney, NSW, Australia 2060 T: (+61) 2 9922-7676 / F: (+61) 2 9922 7989 email: [EMAIL PROTECTED] website: http://www.nusconsulting.com.au -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED] -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]
Re: Perl arrays into MySQL
Crap sorry about the double post.. where's that damn undo send e-mail button :( A 'better' way imo would be, my %record; #full of my stuff my $table = MyTable; my $sql = join ',', map {$_=?} keys %record; $dbh-do(insert into $table set $sql,undef,values %record); Richard - Original Message - From: Daniel Kasak [EMAIL PROTECTED] To: Nik Belajcic [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Tuesday, June 01, 2004 5:15 AM Subject: Re: Perl arrays into MySQL Nik Belajcic wrote: This may be a silly question, but I am wondering if there is something opposite to: @row = $sth-fetchrow_array In other words, instead of fetching rows from MySQL and loading them into an array that can be accessed from Perl, I want to do the opposite - take a Perl (associative) array and load it into MySQL table. I know I could dump it into a text file and read it from there, but assuming I do not want to go through this intermediary step, how could I do it? Any suggestions appreciated. No there is not, but I urge to to implement it :) You can do something like: my %record; # Your record in memory you want in the table. It obviously needs to be filled my $sql_1; my $sql_2; my $sql_3; my $tablename = MyTable; # TODO: Set this another way? foreach my $key (keys %record) { $sql_1 .= $key, ; $sql_2 .= ?, ; $sql_3 .= %record{$key} . , ; } # Chop off the trailing comma and space $sql_1 = substr($sql_1, 0, length($sql_1)-2); $sql_2 = substr($sql_2, 0, length($sql_2)-2); $sql_3 = substr($sql_3, 0, length($sql_3)-2); my $sth = $dbh-prepare(insert into $tablename ( $sql_1 ) values ( $sql_2 )); $sth-execute($sql_3); CAUTIONARY NOTE!! I'm just learning Perl myself. The above code is NOT tested. But you should be able to get the idea anyway :) -- Daniel Kasak IT Developer NUS Consulting Group Level 5, 77 Pacific Highway North Sydney, NSW, Australia 2060 T: (+61) 2 9922-7676 / F: (+61) 2 9922 7989 email: [EMAIL PROTECTED] website: http://www.nusconsulting.com.au -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED] -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]
Re: Perl arrays into MySQL
On 1 Jun 2004, at 2:55 am, Nik Belajcic wrote: This may be a silly question, but I am wondering if there is something opposite to: @row = $sth-fetchrow_array In other words, instead of fetching rows from MySQL and loading them into an array that can be accessed from Perl, I want to do the opposite - take a Perl (associative) array and load it into MySQL table. I know I could dump it into a text file and read it from there, but assuming I do not want to go through this intermediary step, how could I do it? Although it's not elegant, dumping to a text file and reading it in from there is the fastest way to load the data if there's a lot of it. Tim -- Dr Tim Cutts Informatics Systems Group Wellcome Trust Sanger Institute Hinxton, Cambridge, CB10 1SA, UK -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]
Perl arrays into MySQL
This may be a silly question, but I am wondering if there is something opposite to: @row = $sth-fetchrow_array In other words, instead of fetching rows from MySQL and loading them into an array that can be accessed from Perl, I want to do the opposite - take a Perl (associative) array and load it into MySQL table. I know I could dump it into a text file and read it from there, but assuming I do not want to go through this intermediary step, how could I do it? Any suggestions appreciated. -- Nik Belajcic [EMAIL PROTECTED] -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]
Re: Perl arrays into MySQL
Nik Belajcic wrote: This may be a silly question, but I am wondering if there is something opposite to: @row = $sth-fetchrow_array In other words, instead of fetching rows from MySQL and loading them into an array that can be accessed from Perl, I want to do the opposite - take a Perl (associative) array and load it into MySQL table. I know I could dump it into a text file and read it from there, but assuming I do not want to go through this intermediary step, how could I do it? Any suggestions appreciated. No there is not, but I urge to to implement it :) You can do something like: my %record; # Your record in memory you want in the table. It obviously needs to be filled my $sql_1; my $sql_2; my $sql_3; my $tablename = MyTable; # TODO: Set this another way? foreach my $key (keys %record) { $sql_1 .= $key, ; $sql_2 .= ?, ; $sql_3 .= %record{$key} . , ; } # Chop off the trailing comma and space $sql_1 = substr($sql_1, 0, length($sql_1)-2); $sql_2 = substr($sql_2, 0, length($sql_2)-2); $sql_3 = substr($sql_3, 0, length($sql_3)-2); my $sth = $dbh-prepare(insert into $tablename ( $sql_1 ) values ( $sql_2 )); $sth-execute($sql_3); CAUTIONARY NOTE!! I'm just learning Perl myself. The above code is NOT tested. But you should be able to get the idea anyway :) -- Daniel Kasak IT Developer NUS Consulting Group Level 5, 77 Pacific Highway North Sydney, NSW, Australia 2060 T: (+61) 2 9922-7676 / F: (+61) 2 9922 7989 email: [EMAIL PROTECTED] website: http://www.nusconsulting.com.au -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]