php - 将固定长度的文本文件转换为 SQL

标签 php mysql sql

我需要将固定长度的文本文件转换成 MySQL 表。
我最大的问题是每行包含多个单元格,这就是文件发送给我的方式,也是我想要转换它的主要原因。

单元格都是特定长度的;然而,所有内容都包含在一行中。

例如,一行的前 3 个位置 (1 - 3) 是 IRT,接下来的三个位置 (4 - 6) 是 IFTC,接下来的 5 个位置 (7 - 11) 是 FSC,等等。

由于文件最多可包含 300 行记录,我需要一种简单的方法将其直接导入 SQL 表。

我已经在网上搜索了几个小时,试图找到一个解决方案,但是没有逗号分隔,我还没有找到一个可行的解决方案。

如果可能的话,我也想用 PHP 编写这个解决方案。如果有人能给我函数名称,我愿意花很长的时间研究如何使用执行此操作所需的函数,我不希望人们为我编写代码。

最佳答案

文件:

testfile.txt (4 rows)

AAA11111xx
BBB22222yy
CCC33333zz
DDD 444 aa

表:

CREATE TABLE TestLoadDataInfile
( a VARCHAR(3)
, b INT(5)
, c CHAR(2)
) CHARSET = latin1;

代码:

LOAD DATA INFILE 'D:\\...\\testfile.txt'
INTO TABLE TestLoadDataInfile
FIELDS TERMINATED BY ''
LINES TERMINATED BY '\r\n' ;

结果:

mysql> SELECT * FROM TestLoadDataInfile ;
+-----+-------+----+
| a   | b     | c  | 
+-----+-------+----+ 
| AAA | 11111 | xx | 
| BBB | 22222 | yy | 
| CCC | 33333 | zz | 
| DDD |   444 | aa | 
+-----+-------+----+ 

LOAD DATA INFILE 文档在这一点上不是很好(固定大小的字段)。以下是相关部分:

  • If the FIELDS TERMINATED BY and FIELDS ENCLOSED BY values are both empty (''), a fixed-row (nondelimited) format is used. With fixed-row format, no delimiters are used between fields (but you can still have a line terminator). Instead, column values are read and written using a field width wide enough to hold all values in the field. For TINYINT, SMALLINT, MEDIUMINT, INT, and BIGINT, the field widths are 4, 6, 8, 11, and 20, respectively, no matter what the declared display width is.

LINES TERMINATED BY is still used to separate lines. If a line does not contain all fields, the rest of the columns are set to their default values. If you do not have a line terminator, you should set this to ''. In this case, the text file must contain all fields for each row.

Fixed-row format also affects handling of NULL values, as described later. Note that fixed-size format does not work if you are using a multi-byte character set.


NULL handling

With fixed-row format (which is used when FIELDS TERMINATED BY and FIELDS ENCLOSED BY are both empty), NULL is written as an empty string. Note that this causes both NULL values and empty strings in the table to be indistinguishable when written to the file because both are written as empty strings. If you need to be able to tell the two apart when reading the file back in, you should not use fixed-row format.


Some cases are not supported by LOAD DATA INFILE:

  • Fixed-size rows (FIELDS TERMINATED BY and FIELDS ENCLOSED BY both empty) and BLOB or TEXT columns.

  • User variables cannot be used when loading data with fixed-row format because user variables do not have a display width.

关于php - 将固定长度的文本文件转换为 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6564166/

相关文章:

php - 如何使用 PHP 检查远程 HTML 更改?

MySQL 按第一个表中匹配的行排序,然后休息

php - Paypal 集成中的未知错误

php - MYSQL 我应该使用 2 个查询还是将它们添加到我的新表中

php - 多个 PHP mysqli 准备好的语句(INSERT UPDATE SELECT)奇怪的行为

php - 从模型实例 YII 获取模型名称

mysql - 在mysql中填充/加入多对多关系表的多个表

mysql - 同步期间外键为空(错误)

mysql - 从mysql数据库的表中删除行

mysql - SQL子查询导致整体查询变慢