Inserting a Python datetime.datetime object into MySQL

后端 未结 7 1278
Happy的楠姐
Happy的楠姐 2020-11-27 11:26

I have a date column in a MySQL table. I want to insert a datetime.datetime() object into this column. What should I be using in the execute statement?

7条回答
  •  感动是毒
    2020-11-27 11:40

    when iserting into t-sql

    this fails:

    select CONVERT(datetime,'2019-09-13 09:04:35.823312',21)
    

    this works:

    select CONVERT(datetime,'2019-09-13 09:04:35.823',21)
    

    easy way:

    regexp = re.compile(r'\.(\d{6})')
    def to_splunk_iso(dt):
        """Converts the datetime object to Splunk isoformat string."""
        # 6-digits string.
        microseconds = regexp.search(dt).group(1)
        return regexp.sub('.%d' % round(float(microseconds) / 1000), dt)
    

提交回复
热议问题