Forum Moderators: open

Message Too Old, No Replies

Importing empty numeric rows

         

sfast

4:12 pm on Oct 10, 2007 (gmt 0)

10+ Year Member



I have a excel file that contain 14000 rows. There is a numeric column which does n't have values in all the rows. When I try to import it into the table, the rows which dont have any values in the numeric column doesn't get inserted.

How do I make it possible to import the file.

Demaestro

5:17 pm on Oct 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I am guessing the reason it doesn't insert is that you are trying to write "" to an integer field.... I assume this because you indicated that the field is numeric.

If there is no value it can't write an empty string to that field.... if it is indeed a integer field.

Some code to check for it and insert an allowable value should do it.

if (numeric_field.trim() == ""){
numeric_field = 0
}

or

if (numeric_field.trim() == ""){
numeric_field = null
}

If this isn't the problem post back with more info as to how you are importing this file and other such details.. are you getting an error? Does the file just not import? do only parts of it import?

sfast

7:55 pm on Oct 10, 2007 (gmt 0)

10+ Year Member



It might be a dumb question but how will be the above code be tested?
Are you talking about script to import into the database?

Secondly, yes, I am able to import the records that dont have empty fields in numeric columns.

I used a script that I found on MySQL website for importing.

$fcontents = file ('file.csv');

for($i=0; $i<sizeof($fcontents); $i++) {
$line = trim($fcontents[$i], ',');
$arr = explode(",", $line);
#if your data is comma separated
# instead of tab separated,
# change the '\t' above to ','

$sql = "insert into table values ('".
implode("','", $arr) ."')";
mysql_query($sql);
echo $sql ."<br>\n";
if(mysql_error()) {
echo mysql_error() ."<br>\n";
}
}
?>