Results 1 to 5 of 5
  1. #1
    Join Date
    May 2004
    Posts
    105

    Unanswered: Need help using append

    I need to write a SQL insert statement. The column names of the table are in a hash along with the data that I pass to my Insert subroutine. But, I am having a hard time figuring out how to put everything together.

    This is what I have so far:
    #This is my hash
    my ($hwinfo) = @_;

    #Here are the strings that I need to append to the data to make up my SQL statement
    my $str = "Insert into hardware(" ;
    my $str2 = ") values (" ;
    my $str3 = ")\n";

    #In between $str and $str2, I need to list the table's fieldnames located in this code
    foreach my $key (sort keys %$hwinfo){
    print "$key,"
    }

    #Then, I need to append the values of each field between $str2 and $str3
    foreach my $data (sort keys %$hwinfo){
    print "'$hwinfo->{$data}',";
    }

    I need help to putting it together and assigning it to: my ($SQL)

    Thanks,
    Laura

  2. #2
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167
    try this piece of code, but it's untested:
    Code:
    my ($hwinfo) = @_;
    my @sorted = sort{$a cmp $b}keys(%$hwinfo);
    my $statement = 'INSERT INTO hardware('.join(', ',@sorted).') VALUES ('.join(', ',map{${$hw_info}->{$_}}@sorted).')';
    board.perl-community.de - The German Perl-Community

  3. #3
    Join Date
    May 2004
    Posts
    105
    I tried the code but it states that there is no SCALAR reference.

  4. #4
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167
    Hi,

    I'm sorry. I've just checked my code. The correct version is:
    Code:
    my ($hwinfo) = @_;
    my @sorted = sort{$a cmp $b}keys(%$hwinfo);
    my $statement = 'INSERT INTO hardware('.join(', ',@sorted).') VALUES ('.join(', ',map{$hwinfo->{$_}}@sorted).')';
    board.perl-community.de - The German Perl-Community

  5. #5
    Join Date
    May 2004
    Posts
    105
    Thanks.
    -Laura

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •