将数据从测试文件导入mysql数据库

[英]Import data from a test file to the mysql database


I know there are already existing answered questions to my question, but so far, I haven't found one that answers my question 100%.

我知道我的问题已经存在已回答的问题,但到目前为止,我还没有找到一个100%回答我的问题。

Anyway, is there a way to [insert title here] wherein the [delimiter] is the number of characters? What I'm trying to say is...example, the text file has three data: name, age and gender. The format would look like this...

无论如何,有没有办法[在这里插入标题]其中[分隔符]是字符数?我想说的是......例如,文本文件有三个数据:名称,年龄和性别。格式看起来像这样......

Joseph____18_M (pretend the underscores are spaces)

Joseph____18_M(假装下划线是空格)

In the example above, the name has 10 characters, the age has 3 and the gender only has one.

在上面的示例中,名称有10个字符,年龄为3,性别只有一个。

Is there a method in sql to take the first 10 characters in the file, put it on the database, then the next 3 characters, and so on...?

在sql中是否有一个方法可以获取文件中的前10个字符,将其放在数据库中,然后是接下来的3个字符,依此类推......?

Thanks.

谢谢。

1 个解决方案

#1


0  

You should use mysql's load data infile command without specifying a field delimiter or fields optionally enclosed by parameter - but make sure that the file is not a unicode one:

您应该使用mysql的load data infile命令而不指定字段分隔符或可选地由参数括起来的字段 - 但请确保该文件不是unicode文件:

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.

如果FIELDS TERMINATED BY和FIELDS ENCLOSED BY值都为空(''),则使用固定行(非定界)格式。对于固定行格式,字段之间不使用分隔符(但您仍可以使用行终止符)。相反,使用足够宽的字段宽度来读取和写入列值,以保存字段中的所有值。对于TINYINT,SMALLINT,MEDIUMINT,INT和BIGINT,无论声明的显示宽度是多少,字段宽度分别为4,6,8,11和20。

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.

LINES TERMINATED BY仍然用于分隔线。如果某行不包含所有字段,则其余列将设置为其默认值。如果您没有行终止符,则应将其设置为''。在这种情况下,文本文件必须包含每行的所有字段。

Fixed-row format also affects handling of NULL values, as described later.

固定行格式也会影响NULL值的处理,如后面所述。

Note Fixed-size format does not work if you are using a multibyte character set.

注意如果您使用多字节字符集,固定大小格式不起作用。

Declare the name as varchar(10), age probably as varchar(3) or you may try unsigned tinyint, gender as char(1).

声明名称为varchar(10),age可能为varchar(3),或者您可以尝试unsigned tinyint,gender作为char(1)。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/02/09/b01d08deb96a29ff516b536255b2c29a.html



 
  © 2014-2022 ITdaan.com 联系我们: