Friday, September 2, 2011

MySQL default value to integer fields with LOAD DATA from CSV file import with dynamic fields

I was confronted with a problem while loading data from file to the MySQL table. The table have some Integer and Varchar fields and the data is loaded from CSV. Normally the data loading process goes well, but in case the data does not exist for a Integer field, the import breaks and show error like : 
Incorrect integer value: '' for column 'ID' at row 1
The fields are dynamic that I can determine the fields in run-time only. 
So, I devised a solution for this.
I have created the query such that for all the fields with Integer type, I add a '0' with it. 
Here are the steps.