[Typo3] access -> mysql
Kraft Bernhard
kraftb at gmx.net
Wed Mar 9 15:26:29 CET 2005
Ries van Twisk wrote:
> I have been using this VB script to export all tables to CVS.
> I have been trying numerous programs to do a export to mysql but all
> failed one way or the other, or where simply not free (it was a one time
> export).
You could use a php script similar to the following to convert your CVS
into a mysql dump (not testet!):
i assume your CSV uses "," as separator and " (double quote) as delimiter
lets say your CSV look like:
"firstname","lastname","email at server.com"
----------------------<snip>--------------------------------------
<?php
$data = file('mycsv.txt');
foreach ($data as $line) {
// Sanitize line
$line = str_replace(chr(0xa), '', $line);
$line = str_replace(chr(0xd), '', $line);
$line = trim($line);
$parts = explode('","', $line);
// Trim away eventually remaining " at the beginning and end
foreach ($parts as $key => $value) {
if (substr($value, 0, 1) === '"') $value = substr($value, 1);
if (substr($value, -1, 1) === '"') $value = substr($value, 0, -1);
$parts[$key] = $value;
}
// Create insert statement
$sql = 'INSERT INTO mytable SET ';
$sql .= 'firstname="'.$parts[0].'"';
$sql .= ', lastname="'.$parts[1].'"';
$sql .= ', email="'.$parts[2].'"';
$sql .= ';';
echo $sql.chr(10);
}
?>
----------------------</snip>--------------------------------------
Then just save the output and use it as mysql-dump to import it into your DB.
greets,
Bernhard
--
Kraft Bernhard
MOKKA Medienagentur <http://www.mokka.at>
T: +43 - 1 - 895 33 33 - 50
More information about the TYPO3-english
mailing list