开发者

mysql bulk insert a text file

I am having plain text file and i would like to move the content of that text file in to mysql table line by line can any one help me as it is not a csv file how can i achieve this

I designed my db as follows

RowID, int(11), NO, PRI,auto_increment
Text, varchar(94) // Here i have to insert line by line
RecordType, varch开发者_如何学编程ar(50)

Content sample

123456789007
545654665654654
6344534534543534534
786775645654654646
8456546456456546
9078565656546546546456456456


It is not clear do you need to do this once/rarely or as a part of the system.

On the server side, you can do it with

LOAD DATA LOCAL INFILE '<file name>' INTO TABLE <table>;

See the full syntax to

  • change the line terminating string
  • skip rows
  • assign values to RecordType in the same command

EDIT: Specifically in your case

LOAD DATA LOCAL INFILE '<file name>' INTO TABLE <table>
(@Text)
SET Text=@Text 


See LOAD DATA INFILE


Try this

LOAD DATA local INFILE 'C:/Documents and Settings/Administrator/Desktop/Merge.txt' 
INTO TABLE tblachmaster
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES; 


If your file is a CSV-like, you can use a LOAD DATA INFILE statement.

Text file like this (example below), you could load with a Data Import tool (text format) in dbForge Studio for MySQL. It supports headers and skips unnecessary lines.

================================================================
|      actor_id      |     first_name     |     last_name      |
================================================================
|         1          |      PENELOPE      |      GUINESS       |
----------------------------------------------------------------
|         2          |        NICK        |      WAHLBERG      |
----------------------------------------------------------------
|         3          |         ED         |       CHASE        |
----------------------------------------------------------------
|         4          |      JENNIFER      |       DAVIS        |
----------------------------------------------------------------
|         5          |       JOHNNY       |    LOLLOBRIGIDA    |
----------------------------------------------------------------
|         6          |       BETTE        |     NICHOLSON      |
----------------------------------------------------------------
|         7          |       GRACE        |       MOSTEL       |
----------------------------------------------------------------
|         8          |      MATTHEW       |     JOHANSSON      |
----------------------------------------------------------------
|         9          |        JOE         |       SWANK        |
----------------------------------------------------------------
|         10         |     CHRISTIAN      |       GABLE        |
----------------------------------------------------------------


Some PHP script would make an easy way to do this in a flexible and simple way. You'll need to give more details with respect to your text file format and the database schema (some examples from the text file).

Another solution could simply be to build a VBA macro to translate your text file into a valid CSV file. Then use something like phpMyAdmin to upload the CSV file to the DB

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜