Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Inserting Hash Information Into MySQL

by Anonymous Monk
on Jun 20, 2012 at 15:15 UTC ( #977381=note: print w/ replies, xml ) Need Help??


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.


Comment on Re^2: Inserting Hash Information Into MySQL
Download Code
Re^3: Inserting Hash Information Into MySQL
by morgon (Chaplain) 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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2013-05-24 22:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best material for plates (tableware) is:









    Results (516 votes), past polls