Ruby datetime to mysql

I’m missing the boat on datetime. I’m trying to convert Ruby datetime to
be compatible with mysql. I’m not getting any errors but the results
always end up 0000-00-00-0000

I think it may have to do with the mysql requirement for 2 digit month
and hours or the seconds. I’m not sure. When I output my conversion to
my browser the output looks correct.

Can someone shed some light on how to do this.

Thanks

I’m missing the boat on datetime. I’m trying to convert Ruby datetime to
be compatible with mysql. I’m not getting any errors but the results
always end up 0000-00-00-0000

I don’t believe you should need to do any conversion, Ruby’s date types
should work as parameters to prepared statements and date fields
returned by selects shouldn’t need conversions, either. How are you
connecting to mysql, and what problems are you having? It would help
if you posted an example.

-Tim

Hi Tim
I don’t have access to my code at the moment but here is the situation.
I have a simple counter on a web page that updates a customer record 0n
every visit. I can use the mysql built in timestamp however I have
another script that runs daily at midnight which of course updates the
timestamp on all records. So I need to add a datetime string to my
update with a date that mysql understands.

I’m new to Ruby scripting so time and datetime are a bit daunting.

Thanks,
Peter

Hi Again

The one script is eruby index.rhtml the other is not ruby and it does
not use a date or time it simply resets the users quota which also
unfortunately updates the time stamp.

I need to make a string that {#date} is the format that mysql uses,

This is from memory. I connect via dbi to a remote db server on the lan.

dbh.do(“UPDATE member SET counter = counter + 1, ip =
‘#{ENV[‘REMOTE_ADDR’]}’, datetime =’{#date}’ WHERE username =
‘#{ENV[‘REMOTE_USER’]}’”)
end

Peter

I can use the mysql built in timestamp however I have
another script that runs daily at midnight which of course updates the
timestamp on all records. So I need to add a datetime string to my
update with a date that mysql understands.

Assuming their both ruby scripts, I’m not sure I understand why one
would understand ruby time objects, but not the other.

I think to diagnose this further, we really just need to look at how
you’re implementing this, what error messages you’re getting, how
you’re accessing mysql…

Cheers,
-tim

On Feb 28, 10:55 am, peter [email protected] wrote:

I need to make a string that {#date} is the format that mysql uses,

See the (confusingly-tersely-named) Time#strftime method. It will let
you create whatever format you want. You can even wrap it in your own
method, like:

class Time
def as_mysql
strftime “…”
end
end

Gavin K. wrote:

See the (confusingly-tersely-named) Time#strftime method. It will let

strftime is named after the function in the Standard C library. It
creates a _str_ing by _f_ormatting a _t_ime value.

Hi –

On Thu, 1 Mar 2007, Tim H. wrote:

Gavin K. wrote:

See the (confusingly-tersely-named) Time#strftime method. It will let

strftime is named after the function in the Standard C library. It
creates a _str_ing by _f_ormatting a _t_ime value.

Shades of “When you assume…” :slight_smile:

David