gpt4 book ai didi

python - 将空日期填充为 NULL sql 查询

转载 作者:行者123 更新时间:2023-11-29 18:22:43 29 4
gpt4 key购买 nike

我有一个 csv 文件,如下所示:

Dates   Experiment  Experiment_Type RESET_FREQUENCY MEASURE_LENGTH  Value   Date_Integer
2016-09-28 A FORWARD_Detector 1 Minute 1 0.99994 20160928
A FORWARD_Detector 1 Minute 7 0.99959 20160928
2016-09-28 A FORWARD_Detector 1 Minute 14 0.99917 20160928
2016-09-28 A FORWARD_Detector 1 Minute 21 0.99876 20160928
A FORWARD_Detector 1 Minute 30 0.99823 20160928

我收到的内容如下:

Dates   Experiment  Experiment_Type RESET_FREQUENCY MEASURE_LENGTH  Value   Date_Integer
2016-09-28 A FORWARD_Detector 1 Minute 1 0.99994 20160928
0000-00-00 A FORWARD_Detector 1 Minute 7 0.99959 20160928
2016-09-28 A FORWARD_Detector 1 Minute 14 0.99917 20160928
2016-09-28 A FORWARD_Detector 1 Minute 21 0.99876 20160928
0000-00-00 A FORWARD_Detector 1 Minute 30 0.99823 20160928

但我想要的是:

Dates   Experiment  Experiment_Type RESET_FREQUENCY MEASURE_LENGTH  Value   Date_Integer
2016-09-28 A FORWARD_Detector 1 Minute 1 0.99994 20160928
NULL A FORWARD_Detector 1 Minute 7 0.99959 20160928
2016-09-28 A FORWARD_Detector 1 Minute 14 0.99917 20160928
2016-09-28 A FORWARD_Detector 1 Minute 21 0.99876 20160928
NULL A FORWARD_Detector 1 Minute 30 0.99823 20160928

我的查询如下所示:

exp_data = """LOAD DATA LOCAL INFILE 'C:/Data.csv' IGNORE 
INTO TABLE interest_rate_curves_bootstrap
FIELDS TERMINATED BY ';'
LINES TERMINATED BY "\\n"
(@var1,Experiment,Experiment_Type,RESET_FREQUENCY,MEASURE_LENGTH Value, Date_Integer)
set Dates = STR_TO_DATE(@var1, '%d-%b-%y');"""

我尝试了以下方法,但没有成功:

ALTER TABLE experiment_US MODIFY COLUMN experiment_data DATETIME NULL;

然后更新值:

UPDATE experiment_USSET experiment_data= NULL WHERE experiment_data= '0000-00-00';

import sqlalchemy as sqlal

#connection to mysql database
mysql_engine = sqlal.create_engine('mysql+mysqlconnector://xxx@localhost/rates data')


mysql_engine.raw_connection()

metadata = sqlal.MetaData()

experiment_data = sqlal.Table('experiment_data', metadata,
sqlal.Column('Date_String', sqlal.Date(), nullable=True),
sqlal.Column('Experiment', sqlal.String(3), nullable=True),
sqlal.Column('Experiment_Type', sqlal.String(8), nullable=True),
sqlal.Column('RESET_FREQUENCY', sqlal.String(3), nullable=True),
sqlal.Column('MEASURE_LENGHT', sqlal.Integer(), nullable=True),
sqlal.Column('Value', sqlal.Float(), nullable=True),
sqlal.Column('Date_Integer', sqlal.Integer(), nullable=True)
)

exp_data = """LOAD DATA LOCAL INFILE 'C:/Data.csv' IGNORE INTO TABLE experiment_data FIELDS TERMINATED BY ';' LINES TERMINATED BY "\\n" (@var1,Experiment,Experiment_Type,RESET_FREQUENCY,MEASURE_LENGTH Value, Date_Integer) set Dates = STR_TO_DATE(@var1, '%d-%b-%y');"""

mysql_engine.execute(exp_data)

最佳答案

将最后一行更改为:

SET Dates = IF(@var1 = '', NULL, STR_TO_DATE(@var1, '%d-%b-%y'));"""

这会检查 CSV 字段是否为空,然后将 NULL 放入表中,而不是调用 STR_TO_DATE

关于python - 将空日期填充为 NULL sql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46436033/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com