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)
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
, orTIMESTAMP
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.