Php 加载数据填充不使用字段数据中包含编码JSON值的CSV
我有数百万条记录要插入mysql表中。为了快速地将数据迁移到表中,我尝试使用加载数据填充的概念 下面是CSV文件中的数据格式(一些字段包含JSON数据) 当我试图执行代码时,它显示错误如下- SQLSTATE[22032]::3140无效的JSON文本:“在对象成员后缺少逗号或“}”。“user\u day\u record.notesObj”列值中的位置42处。 基本上,当我尝试在CSV中不使用JSON值的情况下执行相同的命令时,它可以完美地工作。但对于文件中的JSON数据(在某些字段中),它会给出错误 这是命令-Php 加载数据填充不使用字段数据中包含编码JSON值的CSV,php,mysql,json,csv,load-data-infile,Php,Mysql,Json,Csv,Load Data Infile,我有数百万条记录要插入mysql表中。为了快速地将数据迁移到表中,我尝试使用加载数据填充的概念 下面是CSV文件中的数据格式(一些字段包含JSON数据) 当我试图执行代码时,它显示错误如下- SQLSTATE[22032]::3140无效的JSON文本:“在对象成员后缺少逗号或“}”。“user\u day\u record.notesObj”列值中的位置42处。 基本上,当我尝试在CSV中不使用JSON值的情况下执行相同的命令时,它可以完美地工作。但对于文件中的JSON数据(在某些字段中),它
LOAD DATA LOCAL INFILE '$file' INTO TABLE user_day_record CHARACTER SET UTF8 FIELDS TERMINATED BY ', ' LINES TERMINATED BY '\n'
有人能帮我在CSV文件的字段中使用json值成功执行命令吗
更新-
下面是user\u day\u记录的结构-
以下是加载包含JSON对象的CSV文件的解决方案。
下面是我们导入时必须使用的额外选项
以“,”结尾的字段
可选地由“\”括起
以“\”转义
以“\”转义
以“\n”结尾的行
*************************** 1. row ***************************
Table: user_day_record
Create Table: CREATE TABLE `user_day_record` (
`dayRecordld` bigint(20) NOT NULL AUTO_INCREMENT,
`id` varchar(191) NOT NULL,
`userld` int(11) NOT NULL,
`daylnMillis` bigint(20) NOT NULL,
`periodStatusActive` tinyint(4) NOT NULL DEFAULT '0',
`ignoreB8T` tinyint(4) NOT NULL DEFAULT '0',
`isOCOff` tinyint(4) NOT NULL DEFAULT '0',
`version` int(11) DEFAULT NULL,
`bbt` json DEFAULT NULL,
`cervicalMucusObj` json DEFAULT NULL,
`cervicalPositionObj` json DEFAULT NULL,
`spotting` json DEFAULT NULL,
`periodStrength` json DEFAULT NULL,
`temperaturelnterferenceObj` json DEFAULT NULL,
`hygieneProductsObj` json DEFAULT NULL,
`pain` json DEFAULT NULL,
`intercourse` json DEFAULT NULL,
`ovulationTestObj` json DEFAULT NULL,
`pregnancyTestObj` json DEFAULT NULL,
`illnessObj` json DEFAULT NULL,
`medicationObj` json DEFAULT NULL,
`notesObj` json DEFAULT NULL,
`sleep` json DEFAULT NULL,
`emotionsObj` json DEFAULT NULL,
`activitiesObj` json DEFAULT NULL,
`oralContraceptives` json DEFAULT NULL,
`digestion` json DEFAULT NULL,
`pExams` json DEFAULT NULL,
`pMyBaby` json DEFAULT NULL,
`pUltrasound` json DEFAULT NULL,
`pVitamin` json DEFAULT NULL,
`pWeight` json DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`dayRecordld`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
mysql> load data local infile '/var/root/temp_Data.csv' into table user_day_record fields terminated by ',' optionally enclosed by '\"' escaped by '\\' escaped by '\"' lines terminated by '\n';
Query OK, 3 rows affected (0.00 sec)
Records: 3 Deleted: 0 Skipped: 0 Warnings: 0
mysql>
mysql> select * from user_day_record\G
*************************** 1. row ***************************
dayRecordld: 1299
id: 1420070400000
userld: 35520
daylnMillis: 1420070400000
periodStatusActive: 0
ignoreB8T: 0
isOCOff: 0
version: 2
bbt: null
cervicalMucusObj: {"types": [3]}
cervicalPositionObj: {"types": [2]}
spotting: {"type": 0, "color": 0, "types": [1], "colors": [2]}
periodStrength: null
temperaturelnterferenceObj: null
hygieneProductsObj: null
pain: null
intercourse: {"type": 0, "types": [], "sexDrive": 0, "sexDrives": [1]}
ovulationTestObj: null
pregnancyTestObj: {"results": [2]}
illnessObj: {"types": [3]}
medicationObj: null
notesObj: null
sleep: null
emotionsObj: {"types": [5]}
activitiesObj: null
oralContraceptives: null
digestion: null
pExams: null
pMyBaby: null
pUltrasound: null
pVitamin: null
pWeight: null
created_at: 2020-01-30 11:48:07
updated_at: 2020-01-30 11:48:07
*************************** 2. row ***************************
dayRecordld: 1300
id: 1420070400000
userld: 48219
daylnMillis: 1420070400000
periodStatusActive: 0
ignoreB8T: 0
isOCOff: 0
version: 2
bbt: null
cervicalMucusObj: null
cervicalPositionObj: null
spotting: null
periodStrength: null
temperaturelnterferenceObj: null
hygieneProductsObj: null
pain: null
intercourse: null
ovulationTestObj: null
pregnancyTestObj: null
illnessObj: null
medicationObj: null
notesObj: null
sleep: null
emotionsObj: null
activitiesObj: null
oralContraceptives: null
digestion: null
pExams: null
pMyBaby: null
pUltrasound: null
pVitamin: null
pWeight: null
created_at: 2020-01-30 11:48:07
updated_at: 2020-01-30 11:48:07
*************************** 3. row ***************************
dayRecordld: 1301
id: 1420070400000
userld: 34938
daylnMillis: 1420070400000
periodStatusActive: 0
ignoreB8T: 0
isOCOff: 0
version: 2
bbt: null
cervicalMucusObj: null
cervicalPositionObj: null
spotting: {"type": 0, "color": 0, "types": [1], "colors": [3]}
periodStrength: null
temperaturelnterferenceObj: null
hygieneProductsObj: {"types": [2]}
pain: {"pain": 0, "pains": [2], "otherPains": [3, 4, 5, 6], "breastSymptom": false, "breastSymptoms": [1]}
intercourse: null
ovulationTestObj: null
pregnancyTestObj: null
illnessObj: null
medicationObj: null
notesObj: null
sleep: null
emotionsObj: null
activitiesObj: null
oralContraceptives: null
digestion: null
pExams: null
pMyBaby: null
pUltrasound: null
pVitamin: null
pWeight: null
created_at: 2020-01-30 11:48:07
updated_at: 2020-01-30 11:48:07
3 rows in set (0.00 sec)
mysql>```
插入时,如果没有JSON值,是否可以显示user\u day\u record
表的结构和表的某些行(user\u day\u record
)?。请编辑问题并添加信息。@wchiquito,我在更新中添加了表格结构截图。请看一看。
*************************** 1. row ***************************
Table: user_day_record
Create Table: CREATE TABLE `user_day_record` (
`dayRecordld` bigint(20) NOT NULL AUTO_INCREMENT,
`id` varchar(191) NOT NULL,
`userld` int(11) NOT NULL,
`daylnMillis` bigint(20) NOT NULL,
`periodStatusActive` tinyint(4) NOT NULL DEFAULT '0',
`ignoreB8T` tinyint(4) NOT NULL DEFAULT '0',
`isOCOff` tinyint(4) NOT NULL DEFAULT '0',
`version` int(11) DEFAULT NULL,
`bbt` json DEFAULT NULL,
`cervicalMucusObj` json DEFAULT NULL,
`cervicalPositionObj` json DEFAULT NULL,
`spotting` json DEFAULT NULL,
`periodStrength` json DEFAULT NULL,
`temperaturelnterferenceObj` json DEFAULT NULL,
`hygieneProductsObj` json DEFAULT NULL,
`pain` json DEFAULT NULL,
`intercourse` json DEFAULT NULL,
`ovulationTestObj` json DEFAULT NULL,
`pregnancyTestObj` json DEFAULT NULL,
`illnessObj` json DEFAULT NULL,
`medicationObj` json DEFAULT NULL,
`notesObj` json DEFAULT NULL,
`sleep` json DEFAULT NULL,
`emotionsObj` json DEFAULT NULL,
`activitiesObj` json DEFAULT NULL,
`oralContraceptives` json DEFAULT NULL,
`digestion` json DEFAULT NULL,
`pExams` json DEFAULT NULL,
`pMyBaby` json DEFAULT NULL,
`pUltrasound` json DEFAULT NULL,
`pVitamin` json DEFAULT NULL,
`pWeight` json DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`dayRecordld`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
mysql> load data local infile '/var/root/temp_Data.csv' into table user_day_record fields terminated by ',' optionally enclosed by '\"' escaped by '\\' escaped by '\"' lines terminated by '\n';
Query OK, 3 rows affected (0.00 sec)
Records: 3 Deleted: 0 Skipped: 0 Warnings: 0
mysql>
mysql> select * from user_day_record\G
*************************** 1. row ***************************
dayRecordld: 1299
id: 1420070400000
userld: 35520
daylnMillis: 1420070400000
periodStatusActive: 0
ignoreB8T: 0
isOCOff: 0
version: 2
bbt: null
cervicalMucusObj: {"types": [3]}
cervicalPositionObj: {"types": [2]}
spotting: {"type": 0, "color": 0, "types": [1], "colors": [2]}
periodStrength: null
temperaturelnterferenceObj: null
hygieneProductsObj: null
pain: null
intercourse: {"type": 0, "types": [], "sexDrive": 0, "sexDrives": [1]}
ovulationTestObj: null
pregnancyTestObj: {"results": [2]}
illnessObj: {"types": [3]}
medicationObj: null
notesObj: null
sleep: null
emotionsObj: {"types": [5]}
activitiesObj: null
oralContraceptives: null
digestion: null
pExams: null
pMyBaby: null
pUltrasound: null
pVitamin: null
pWeight: null
created_at: 2020-01-30 11:48:07
updated_at: 2020-01-30 11:48:07
*************************** 2. row ***************************
dayRecordld: 1300
id: 1420070400000
userld: 48219
daylnMillis: 1420070400000
periodStatusActive: 0
ignoreB8T: 0
isOCOff: 0
version: 2
bbt: null
cervicalMucusObj: null
cervicalPositionObj: null
spotting: null
periodStrength: null
temperaturelnterferenceObj: null
hygieneProductsObj: null
pain: null
intercourse: null
ovulationTestObj: null
pregnancyTestObj: null
illnessObj: null
medicationObj: null
notesObj: null
sleep: null
emotionsObj: null
activitiesObj: null
oralContraceptives: null
digestion: null
pExams: null
pMyBaby: null
pUltrasound: null
pVitamin: null
pWeight: null
created_at: 2020-01-30 11:48:07
updated_at: 2020-01-30 11:48:07
*************************** 3. row ***************************
dayRecordld: 1301
id: 1420070400000
userld: 34938
daylnMillis: 1420070400000
periodStatusActive: 0
ignoreB8T: 0
isOCOff: 0
version: 2
bbt: null
cervicalMucusObj: null
cervicalPositionObj: null
spotting: {"type": 0, "color": 0, "types": [1], "colors": [3]}
periodStrength: null
temperaturelnterferenceObj: null
hygieneProductsObj: {"types": [2]}
pain: {"pain": 0, "pains": [2], "otherPains": [3, 4, 5, 6], "breastSymptom": false, "breastSymptoms": [1]}
intercourse: null
ovulationTestObj: null
pregnancyTestObj: null
illnessObj: null
medicationObj: null
notesObj: null
sleep: null
emotionsObj: null
activitiesObj: null
oralContraceptives: null
digestion: null
pExams: null
pMyBaby: null
pUltrasound: null
pVitamin: null
pWeight: null
created_at: 2020-01-30 11:48:07
updated_at: 2020-01-30 11:48:07
3 rows in set (0.00 sec)
mysql>```