pat.inside
pat.inside

Reputation: 1834

How to insert a string(Fri Nov 19 16:23:54 +0800 2010) into a table field whose type is TIMESTAMP?

I'm using Python and MYSQLdb to connect to Mysql. and now i need to insert a string "Fri Nov 19 16:23:54 +0800 2010" into a field, how can i do?

Upvotes: 0

Views: 141

Answers (1)

Michał Niklas
Michał Niklas

Reputation: 54342

You can parse that sting with Python time.strptime() method and then convert it to string required by MySQL using strftime(). Unfortunately my Python 2.6 cannot handle %z directive, but you can check on your implementation that code:

import datetime

def test_dt(d, f):
    dt = datetime.datetime.strptime(d, f)
    print('%s -> %s' % (d, dt.strftime('%Y-%m-%d %H:%M:%S %Z')))
    print('-' * 10)

test_dt('Fri Nov 19 16:23:54 2010', '%a %b %d %H:%M:%S %Y')
test_dt('Fri Nov 19 16:23:54 +8000 2010', '%a %b %d %H:%M:%S %z %Y')

If it fails with %z then think if you need that time zone, or if you can remove it from string and apply it to dt object other way.

It seems that %z issue was reported and solved: http://bugs.python.org/issue6641

Upvotes: 1

Related Questions