在Python中转换字符串以插入到MySQL DB日期列日期 [英] Convert String In Python to insert into MySQL DB date column date

查看:345
本文介绍了在Python中转换字符串以插入到MySQL DB日期列日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不寻常的日期字符串,我想插入一个MySQL数据库。

I have an unusual date string that I want to insert into a MySQL DB.

date = 'Thu, 14 Mar 2013 13:33:07 -0400'

这里是insert语句

And here is the insert statement

self.cursor.execute("INSERT INTO table1 (`DATE`)VALUES (%s);",(date))

当我这样做时,它显示在数据库中,如:

When I do it like this it shows up in the database like :

0000-00-00 00:00:00

即使我进入SQL手册,它显示相同的方式。

Even if I enter the SQL manualy it shows up the same way.

如何将日期字符串转换为插入的mysql可读日期?

How do I convert the date string into a mysql readable date for insert?

推荐答案

from dateutil.parser import parse
date = 'Thu, 14 Mar 2013 13:33:07 -0400'
parse(date).strftime("%Y-%m-%d %H:%M:%S")

这篇关于在Python中转换字符串以插入到MySQL DB日期列日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆