If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > importing a text file

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-19-04, 17:54
bosewicht1 bosewicht1 is offline
Registered User
 
Join Date: Sep 2004
Posts: 16
importing a text file

i have a pretty long text file that i need to get into a db. The are line 1: Name, line 2: address, line 3:city, and line 4hone number. What would be the easiest way to get this into a db? would it be easier if i make them comma-delimited ?
Reply With Quote
  #2 (permalink)  
Old 12-19-04, 18:44
yellowmarker yellowmarker is offline
Registered User
 
Join Date: Jul 2004
Location: Dundee, Scotland
Posts: 107
technically the file can be read either way. I'd imagine it would be an issue to re-format the text file so your solution is to read the text file line by line, building up SQL insert statements and executing them.

// to read the text file line by line using fgets ...

// note: Returns a string of up to length - 1 bytes read from the file pointed to by handle. Reading ends when length - 1 bytes have been read, on a newline (which is included in the return value), or on EOF (whichever comes first). If no length is specified, the length defaults to 1k, or 1024 bytes.

$handle = fopen("/tmp/inputfile.txt", "r");
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
echo $buffer;
}
fclose($handle);



// if the text file was in csv format using @fgetcsv ...

$fp = @fopen($_FILES['csvfile']['tmp_name'], "r") or die ("Cannot open file on server");

while(!@feof ($fp)) {
$row=@fgetcsv($fp, 1024, ",");
}

fclose($fp);
Reply With Quote
  #3 (permalink)  
Old 12-20-04, 00:51
Chris J. Chris J. is offline
Registered User
 
Join Date: Dec 2004
Posts: 2
I had to do this with a bunch of text files (newspaper articles).

I used this from the command line:

LOAD DATA LOCAL INFILE 'files.txt' INTO TABLE media FIELDS TERMINATED BY '\r\n%%\r\n' LINES TERMINATED BY '\n%%%\n' (date,type,headline,text);

The \r\n stands for looking for a skip to the next line.
You can use whatever you want to separate the fields, like commas, but I chose two percent symbols on their own line, like this (since the articles had commas in them):

Date
%%
Type
%%
Headline
%%
Text
%%%
Date
%%
Headline
%%
etc.

Just make sure the file files.txt is in the same directory that you're running mysql from.

CHRIS

Last edited by Chris J.; 12-20-04 at 00:55. Reason: clarify separating lines
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On