Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: Writing NULL values to a MySQL record via DBI

by Anonymous Monk
on Feb 26, 2015 at 22:34 UTC ( [id://1118008]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Writing NULL values to a MySQL record via DBI
in thread Writing NULL values to a MySQL record via DBI

I'll do some of the work for you:
my $mo_raw_sql = " REPLACE INTO met_office_raw_data SET observation_datetime_utc = ?, observation_datetime_local = ? "; my $mo_raw = $dbh->do( $mo_raw_sql, undef, $file_obs_datetime_utc, $file_obs_datetime_local, );
Just fill out the rest and you should be fine, so long as your schema definition allows NULL values.

Replies are listed 'Best First'.
Re^4: Writing NULL values to a MySQL record via DBI
by ureco (Acolyte) on Feb 26, 2015 at 22:49 UTC
    Just started to search for placeholders and I can see what you are suggesting - thanks. As I understand it (remember I've only read a small amount in the last few minutes) you substitute the ? for the corresponding variable in the statement. In the case above, first ? relates to the $file_obs_datetime_utc and the second ? relates to $file_obs_datetime_local. Does the undef statement above cover all the fields that follow? Just looking to understand fully the code you posted.
      Please read the documentation. The undef is very necessary here and refers only to the value for the second argument being passed to the method do. Explaining exactly what that argument is for is difficult, at best. Otherwise you are very very close to understanding how this works.
        I might not be getting this fully - I'm trying to go through the cpan documentation for DBI. If you want to insert a NULL value for a particular field you use 'undef' but what if in my case you don't know whether the field will be undefined when you extract if from the CSV file? I need the variable (e.g. '$file_temp_c' from the original code block I included earlier) to be equal to either a value if populated or 'undef' if the CSV file did not have a value. I can test the variable and they do indicate they are undefined but, at least in my original code they do not translate to NULL in the MySQL table - hope this makes sense and is possible.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-03-29 07:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found