12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- #!/usr/bin/php
- <?php
- define('CHARSET', 'UTF8');
- define('FIELD_SEP', "\t");
- ini_set("memory_limit", '32M');
- define('ROWSPERLINE', 3000);
- $tablename=$argv[1];
- $f = fopen($argv[2], "r");
- if(!$f) die("Cannot open file");
- $line=trim(fgets($f,65535));
- $words=explode(FIELD_SEP, $line);
- $fields_count = count($words);
- print "SET NAMES ".CHARSET.";\n\n";
- print "DROP TABLE IF EXISTS `".$tablename."`;\n";
- print "CREATE TABLE `".$tablename."` (\n";
- print "\t`".implode("` varchar(255),\n\t`", $words).'` varchar(255)';
- print "\n) CHARSET=".CHARSET.";\n\n";
- $fieldsstr='`'.implode("`,`",$words).'`';
- $linen=0;
- while(!feof($f)) {
- if(!($linen%ROWSPERLINE))
- $outline = 'INSERT INTO `'.$tablename.'` ('.$fieldsstr.')VALUES';
- $outline .= '(';
- $line = str_replace('"', '\"', trim(fgets($f, 65536)));
- $words = explode(FIELD_SEP, $line);
- while (count($words) < $fields_count)
- $words[] = '';
- $outline .='"'.implode('","',$words).'"';
- $outline .= "),";
- $linen++;
- if(!($linen%ROWSPERLINE))
- print substr($outline, 0, -1).";\n";
- }
- if($linen%ROWSPERLINE)
- print substr($outline, 0, -1).";\n";
- fwrite(STDERR, "rows: ".$linen."\n");
- fclose($f);
- ?>
|