Greg, I agree with Tom H.
PHP can also do an excellent job of this, even as a standalone
script (ie no web server interaction). And you won't have to figure
out how to load the DBI module!  I'll adapt an example
from something else I did:  (Warning, not tested code...)

#!/usr/local/bin/php
<?php
        $filecontents = file("filename");
        $numsucc = 0;  $numtotal = 0;
         $vars = array();
        foreach( $filecontents as $line) {
                $Qstart = "INSERT INTO dbname SET ";
                $COLS = "";
                while (1) {
                        $line = trim($line);
                        if (! strlen($line)) break; // next story
                        list($tag,$content) = preg_split("/:/", $line );

                        $vars[$tag] = $content;
                        $COLS .= "$tag=$content, ";
                }
                $QUERY = $Qstart . substr($COLS,0,-2);
                        // remove trailing comma and space
                $numtotal++;
                $result = mysql_query($QUERY);
                if ($result) $numsucc++;
                
        }
        echo "All done!  $numsucc of $numtotal records successfully inserted\n";
?>

Greg Peretti wrote:

> This is my first post on this list, so bear with me.
> 
> I have a text file with 10,000 stories in it (about 130 MB). I would
> like to break it up into 10,000 separate files and am trying to figure
> out a way to feed it into a MYSQL database.
> 
> I have set up the database with the pertinent fields, except the
> individual stories do not necessarily follow the same pattern.
> 
> For instance, almost all of them have:
> 
> Header
> Publication
> section
> edition
> date
> page number
> headline
> byline
> story
> 
> all separated by two returns, with each overall story separated by four
> returns.
> 
> Now, I can load the file using fields terminated by "\n\n" and lines
> terminated by "\n\n\n\n", but if, for instance, a story does not have a
> byline, I'm in trouble. It will happily put the story data in the byline
> field.
> 
> Am I out of luck, or can I specify text to look for in specific fields
> as the file is being loaded, i.e. the date field is always preceded by
> "Date:" and if no "Date:" is found, skip the field?
> 
> I'm using 3.23.44
> 
> 
> --
> 
> Greg Peretti
> web developer
> www.abqjournal.com
> (505) 823-3888
> 
> -----------------------------------
> 
> The web of our life is of a mingled yarn, good and ill together.
> -  William Shakespeare
> 
> 
> 
> ---------------------------------------------------------------------
> Before posting, please check:
>    http://www.mysql.com/manual.php   (the manual)
>    http://lists.mysql.com/           (the list archive)
> 
> To request this thread, e-mail <[EMAIL PROTECTED]>
> To unsubscribe, e-mail <[EMAIL PROTECTED]>
> Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php
> 
> 
> 


-- 
Steve Rapaport
World Citizen


---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to