带有空字段的 str_to_date() 函数出现问题
Issue with str_to_date() function with Empty field
在 python 中,我试图将一些数据插入到我的 table;
try:
cursor.execute("INSERT INTO covid_testtable (pid, age, state, city, notes, backnotes, type, nationality, status, announced, changed) " +
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, STR_TO_DATE(%s, '%%d/%%m/%%Y'), STR_TO_DATE(%s, '%%d/%%m/%%Y'))",
(pid, age, item["detectedstate"], item["detectedcity"], item["notes"], item["backupnotes"], item["typeoftransmission"],
item["nationality"], item["currentstatus"], dateannounced, statuschanged));
except ValueError as verr:
print(item["dateannounced"], verr);
break;
这一直有效,直到其中一个日期列有一个空条目。
pymysql.err.InternalError: (1411, "Incorrect datetime value: '' for function str_to_date"
我试图将空日期的值更改为“00/00/0000”,但问题似乎出在 str_to_date()函数。
我可以插入 NULL 值,但是无法插入。
成功插入的有效值的格式为“30/01/2020”->“%d/%m/%Y"
我的 table 架构如下:
create table covid_testtable ( pid int NOT NULL, age int, state VARCHAR(255),
city VARCHAR(255), notes VARCHAR(255), backnotes VARCHAR(255), type VARCHAR(255),
nationality VARCHAR(255), status VARCHAR(255), announced DATE default NULL,
changed DATE default NULL, PRIMARY KEY (pid));
编辑 2:选择的答案解决了这个问题:
def validate_date(val):
try:
return datetime.strptime(val, '%d/%m/%Y').strftime('%Y-%m-%d');
except ValueError as verr:
return None;
dateannounced = validate_date(item["dateannounced"]);
statuschanged = validate_date(item["statuschangedate"]);
try:
cursor.execute("INSERT INTO covid_testtable (pid, age, state, city, notes, backnotes, type, nationality, status, announced, changed) " +
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
(pid, age, item["detectedstate"], item["detectedcity"],
item["notes"], item["backupnotes"], item["typeoftransmission"],
item["nationality"], item["currentstatus"], dateannounced,
statuschanged));
用if()
检查字符串是否为空。
cursor.execute("""
INSERT INTO covid_testtable (pid, age, state, city, notes, backnotes, type, nationality, status, announced, changed)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s,
IF(%s = '', NULL, STR_TO_DATE(%s, '%%d/%%m/%%Y')),
IF(%s = '', NULL, STR_TO_DATE(%s, '%%d/%%m/%%Y')))""",
(pid, age, item["detectedstate"], item["detectedcity"], item["notes"], item["backupnotes"], item["typeoftransmission"],
item["nationality"], item["currentstatus"], dateannounced, dateannounced, statuschanged, statuschanged));
请注意,我不得不在参数列表中重复 dateannounced
和 statuschanged
,因为除了 STR_TO_DATE
之外,我还要将它们替换到 IF
函数中函数。
您可以在 python 代码中预处理它们,而不是处理查询中的日期。例如:
from datetime import datetime
dstr = ''
try:
dateannounced = datetime.strptime(dstr, '%d/%m/%Y').strftime('%Y-%m-%d')
except ValueError:
dateannounced = None
那么您的查询就变成了
cursor.execute("INSERT INTO covid_testtable (pid, age, state, city, notes, backnotes, type, nationality, status, announced, changed) " +
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
(pid, age, item["detectedstate"], item["detectedcity"],
item["notes"], item["backupnotes"], item["typeoftransmission"],
item["nationality"], item["currentstatus"], dateannounced,
statuschanged));
在 python 中,我试图将一些数据插入到我的 table;
try:
cursor.execute("INSERT INTO covid_testtable (pid, age, state, city, notes, backnotes, type, nationality, status, announced, changed) " +
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, STR_TO_DATE(%s, '%%d/%%m/%%Y'), STR_TO_DATE(%s, '%%d/%%m/%%Y'))",
(pid, age, item["detectedstate"], item["detectedcity"], item["notes"], item["backupnotes"], item["typeoftransmission"],
item["nationality"], item["currentstatus"], dateannounced, statuschanged));
except ValueError as verr:
print(item["dateannounced"], verr);
break;
这一直有效,直到其中一个日期列有一个空条目。
pymysql.err.InternalError: (1411, "Incorrect datetime value: '' for function str_to_date"
我试图将空日期的值更改为“00/00/0000”,但问题似乎出在 str_to_date()函数。 我可以插入 NULL 值,但是无法插入。
成功插入的有效值的格式为“30/01/2020”->“%d/%m/%Y"
我的 table 架构如下:
create table covid_testtable ( pid int NOT NULL, age int, state VARCHAR(255),
city VARCHAR(255), notes VARCHAR(255), backnotes VARCHAR(255), type VARCHAR(255),
nationality VARCHAR(255), status VARCHAR(255), announced DATE default NULL,
changed DATE default NULL, PRIMARY KEY (pid));
编辑 2:选择的答案解决了这个问题:
def validate_date(val):
try:
return datetime.strptime(val, '%d/%m/%Y').strftime('%Y-%m-%d');
except ValueError as verr:
return None;
dateannounced = validate_date(item["dateannounced"]);
statuschanged = validate_date(item["statuschangedate"]);
try:
cursor.execute("INSERT INTO covid_testtable (pid, age, state, city, notes, backnotes, type, nationality, status, announced, changed) " +
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
(pid, age, item["detectedstate"], item["detectedcity"],
item["notes"], item["backupnotes"], item["typeoftransmission"],
item["nationality"], item["currentstatus"], dateannounced,
statuschanged));
用if()
检查字符串是否为空。
cursor.execute("""
INSERT INTO covid_testtable (pid, age, state, city, notes, backnotes, type, nationality, status, announced, changed)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s,
IF(%s = '', NULL, STR_TO_DATE(%s, '%%d/%%m/%%Y')),
IF(%s = '', NULL, STR_TO_DATE(%s, '%%d/%%m/%%Y')))""",
(pid, age, item["detectedstate"], item["detectedcity"], item["notes"], item["backupnotes"], item["typeoftransmission"],
item["nationality"], item["currentstatus"], dateannounced, dateannounced, statuschanged, statuschanged));
请注意,我不得不在参数列表中重复 dateannounced
和 statuschanged
,因为除了 STR_TO_DATE
之外,我还要将它们替换到 IF
函数中函数。
您可以在 python 代码中预处理它们,而不是处理查询中的日期。例如:
from datetime import datetime
dstr = ''
try:
dateannounced = datetime.strptime(dstr, '%d/%m/%Y').strftime('%Y-%m-%d')
except ValueError:
dateannounced = None
那么您的查询就变成了
cursor.execute("INSERT INTO covid_testtable (pid, age, state, city, notes, backnotes, type, nationality, status, announced, changed) " +
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
(pid, age, item["detectedstate"], item["detectedcity"],
item["notes"], item["backupnotes"], item["typeoftransmission"],
item["nationality"], item["currentstatus"], dateannounced,
statuschanged));