Page 1 of 1

import csv and choose the columns

Posted: Mon Aug 23, 2010 5:20 pm
by peterhall
hi there. I have a csv file with 30 columns, but i just want to import 5 columns, and they aren't near to each other.

eg:

col1 col2 col3 col4 col5 col6 col7 col8 col9 col10

I want to import col1, col2, col4, col7 and col9 and the first column in the table is increment...

How can I do that???

tk u

Re: import csv and choose the columns

Posted: Tue Aug 24, 2010 12:15 pm
by peterhall
Hi there, Just to inform that I did it... below theres the code that I use to make it.
What he does? creates a temporary table,imports the csv, append the data into the fixed table, delete the temporary table and delete
the lines where the result is = to "something", to avoid the null lines.

It maybe a litle newbie, but it works.

besides that, I build this one with parts of another ones, so, the credit isn't mine, just the structure!!!

Code: Select all

<?php
// this scrip allows to import data from a csv file into a table in mysql
// creating a temporary table, import data to temporary table
// querying the data choosing the fields to paste
// insert the data into the main table
// delete the temporary table, saving data base space
// delete the lines with the CI = 0


$con = mysql_connect("localhost","root","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// step one: create temporary table
mysql_select_db("tool_nsn", $con);
$sql = "CREATE TABLE table_temp
(
field1 int,
field2 varchar(50),
field3 varchar(25)
   )";

// execute query
mysql_query($sql,$con);

mysql_close($con);
?> 


<?php
// step two: import data from csv file

$databasehost = "localhost";
$databasename = "db_name";
$databasetable = "table_temp";
$databaseusername ="root";
$databasepassword = "password";

$fieldseparator = ",";
$lineseparator = "\n";
$csvfile = "/dir/dir/file.csv";
$addauto = 0;

// if you want to save the mysql queries in a file set $save to 1.
// permission on the file should be set to 777, either upload a sample file through ftp and
// change the permissions, or execute at the prompt: touch output.sql && chmod 777 output.sql

$save = 0;
$outputfile = "output.sql";

if(!file_exists($csvfile)) {
	echo "File not found. Make sure you specified the correct path.\n";
	exit;
}

$file = fopen($csvfile,"r");

if(!$file) {
	echo "Error opening data file.\n";

	exit;
}

$size = filesize($csvfile);

if(!$size) {
	echo "File is empty.\n";
	exit;
}

$csvcontent = fread($file,$size);

fclose($file);

$con = @mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
@mysql_select_db($databasename) or die(mysql_error());
$lines = 0;
$queries = "";
$linearray = array();

foreach(split($lineseparator,$csvcontent) as $line) {
	$lines++;
	$line = trim($line," \t");
	$line = str_replace("\r","",$line);

	// this line escapes the special character. remove it if entries are already escaped in the csv file
	$line = str_replace("'","\'",$line);

	$linearray = explode($fieldseparator,$line);
	$linemysql = implode("','",$linearray);
	if($addauto)
		$query = "insert into $databasetable values('','$linemysql');";
	else
		$query = "insert into $databasetable values('$linemysql');";

	$queries .= $query . "\n";
	@mysql_query($query);
}

@mysql_close($con);

if($save) {

	if(!is_writable($outputfile)) {
		echo "File is not writable, check permissions.\n";
	}
	else {

		$file2 = fopen($outputfile,"w");

		if(!$file2) {

			echo "Error writing to the output file.\n";

		}

		else {

			fwrite($file2,$queries);

			fclose($file2);

		}

	}

	

}

echo "Found a total of $lines records in this csv file.\n";

?>

<?php

// step three: append the data from the temporary table to the main table

$con = mysql_connect("localhost","root","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("db_name", $con);

$result = mysql_query("INSERT INTO motorola_table_fixmfK ( field1, field2, field3)\n"
    . "SELECT table_temp.field1, table_temp.field2, table_temp.field3\n"
    . "FROM tables_temp;");
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

?> 

<?php

// step four: delete the temporary table

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = password;
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully<br />';
$sql = "DROP TABLE table_temp";
mysql_select_db( '' );db_name
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not delete table: ' . mysql_error());
}
echo "Table deleted successfully\n";
mysql_close($conn);
?>

<?php  

// step five: delete the lines where id it's equal to "0"

$con = mysql_connect("localhost","root","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("db_name", $con);

$query = "DELETE FROM `table_fix` WHERE id='$_GET[0]'";  

$deleta = mysql_query($query) or print(mysql_error());  

exit;  

?>