將數據從測試文件導入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 联系我们: