in reply to Re: Inserting Hash Information Into MySQL
in thread Inserting Hash Information Into MySQL

I am using strict. The hash uses the values in @id as the key, which is why I used $id. My apologies - the code to enter into MySQL is:
my $insert_query = "INSERT INTO test(id, stats) VALUES (?, ?);"; $sth = $dbh->prepare($insert_query); while(($key, $value) = each(%hash)){ $sth->execute($key, $value); }
It inserts the ID without a problem. The only issue is that arrays can't be stored in MySQL, but I don't know how to get the information out of the array to store it otherwise.

Replies are listed 'Best First'.
Re^3: Inserting Hash Information Into MySQL
by morgon (Priest) on Jun 20, 2012 at 15:43 UTC
    It seems to me that the values in your %hash are array-refs.

    So you cannot simply insert them into the table as is.

    I am not sure what you are trying to achieve but I assume you want something like this:

    while(($key, $value) = each(%hash)){ for my $stat_value (@$value) { $sth->execute($key, $stat_value); } }
      That was what I was looking for, and it worked perfectly. Thank you! I'm sorry for being unclear about what I was trying to achieve.