Imported data shows 0000-00-00 format

问题: Imported data (only date and execution_period that dd.mm.yyyy) from Excel in MySQL shows 0000-00-00. I tried to change dtypes of these columns to datetime (by using pd.to_d...

问题:

Imported data (only date and execution_period that dd.mm.yyyy) from Excel in MySQL shows 0000-00-00. I tried to change dtypes of these columns to datetime (by using pd.to_datetime()), then exported it in Excel.

df['columnName'] = pd.to_datetime(df['columnName'])

It didn't help. Generally I'm trying to import data from Excel to MySQL.

query = """
INSERT INTO sanction (
    id,
    organization_type, organization, date,
    decision_number, penalty_type, penalty_way,
    penalty, violation, execution_period,
    article, note, type_npa,
    department, uploaded_date)
VALUES (
    null,
    %s, %s, %s,
    %s, %s, %s,
    %s, %s, %s,
    %s, %s, %s,
    %s, %s)
"""

for r in range(1, sheet.nrows):
    organization_type = sheet.cell(r, 1).value
    organization = sheet.cell(r, 2).value
    date = sheet.cell(r, 3).value
    decision_number = sheet.cell(r, 4).value
    penalty_type = sheet.cell(r, 5).value
    penalty_way = sheet.cell(r, 6).value
    penalty = sheet.cell(r, 7).value
    violation = sheet.cell(r, 8).value
    execution_period = sheet.cell(r, 9).value
    article = sheet.cell(r, 10).value
    note = sheet.cell(r, 11).value
    type_npa = sheet.cell(r, 12).value
    department = sheet.cell(r, 13).value
    uploaded_date = datetime.now().strftime("%Y-%m-%d %H:%M")

values = (
    # the first value of the INSERT statement will be NULL
    organization_type, organization, date,              # 3 elements
    decision_number, penalty_type, penalty_way,         # 3 elements
    penalty, violation, execution_period,               # 3 elements
    article, note, type_npa,                            # 3 elements
    department, uploaded_date,                          # 2 elements
)
mycursor.execute(query, values)

回答1:

The '0000-00-00' values in your inserted data indicates that MySQL failed to convert the value you passed to a date.

When turning a string to a date datatype, MySQL basically assumes format yyyy-mm-dd (or yyyy-mm-dd hh24:mi:ss for datetime), with some added flexibility. It does not recognizes your input format (dd.mm.yyyy). From the documentation :

Invalid DATE, DATETIME, or TIMESTAMP values are converted to the “zero” value of the appropriate type ('0000-00-00' or '0000-00-00 00:00:00').

One solution would be to do explicit conversion in the query with the STR_TO_DATE() function, like :

INSERT INTO sanction (
    id,
    organization_type, organization, date,
    decision_number, penalty_type, penalty_way,
    penalty, violation, execution_period,
    article, note, type_npa,
    department, uploaded_date)
VALUES (
    null,
    %s, %s, STR_TO_DATE(%s, '%d.%m.%Y'),
    %s, %s, %s,
    %s, %s, %s,
    %s, %s, %s,
    %s, %s)
  • 发表于 2019-02-24 00:22
  • 阅读 ( 168 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除