2

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, varchar(50)

Content sample

123456789007
545654665654654
6344534534543534534
786775645654654646
8456546456456546
9078565656546546546456456456
4
  • Can you be more precise then 'line by line'. I assume you mean that the text file is actually single column (text). If so, you can consider your text file CSV (even if it contains no commas). Commented Apr 8, 2011 at 8:19
  • Not a single column it has multiple columns Commented Apr 8, 2011 at 8:30
  • Well if it has multiple columns, then pls provide with the sample of for the text file. Then the answers will be of better quality. Commented Apr 8, 2011 at 8:57
  • In your content sample I can see only single column. Rows are multiple. Safe to consider it CSV (or tab delimited, or whatever delimited since there are no multiple columns and no delimiters). Commented Apr 8, 2011 at 9:18

5 Answers 5

3

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 
Sign up to request clarification or add additional context in comments.

4 Comments

Unreason i write this LOAD DATA INFILE 'c:\ACH1.txt' INTO TABLE tblACHDATA i am getting an error as c:ACH1.txt not found
LOCAL is significant, try that.
Semms windoze expect backslashes to be forward slashes, C:\ to be C:/ (I don't run mysql on win). Google is your friend.
Read the docs from dev.mysql.com/doc/refman/5.1/en/load-data.html, the section on LOCAL. If using LOCAL location of your file should be available to client, if no LOCAL the to the server. But read the details on relative paths, then there's also security and permissions of the mysql process, etc... Bottom line is that this works and mysql docs might not be perfect, but are not wrong. Use them.
2

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; 

Comments

2

See LOAD DATA INFILE

Comments

1

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        |
----------------------------------------------------------------

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.