From e8d4221acca69167761b350fe2b1c82f8096ffb3 Mon Sep 17 00:00:00 2001 From: Jujhar Date: Fri, 17 Oct 2014 14:10:49 -0700 Subject: [PATCH 1/6] Added some base code --- README | 11 ++++++++++- php-csv2mysql.php | 12 +++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/README b/README index b0cb50f..3800107 100644 --- a/README +++ b/README @@ -1,6 +1,15 @@ +#php-csv2mysql + Chris Armstrong chris@chrisarmstrong.me Converts CSV files to .SQL files, with a CREATE TABLE and INSERT statements for each row of the CSV. Will automatically ignore columns that contain no data, and will make all columns type VARCHAR with a size equal to the length of that column's longest value. -TO USE: php csv_to_sql.php inputfile.csv outputfile.sql database_name + +## Instructions +Replace the mysql connect database and password name the applicaion. +to use: php csv_to_sql.php inputfile.csv outputfile.sql database_name + +## Under development +optional commands: --nopk + diff --git a/php-csv2mysql.php b/php-csv2mysql.php index 0842be4..0bcf368 100644 --- a/php-csv2mysql.php +++ b/php-csv2mysql.php @@ -3,6 +3,7 @@ $IMPORT_FILE = $argv[1]; $EXPORT_FILE = $argv[2]; $DB_NAME = $argv[3]; + $OPTION = $argv[4]; $PK_INDEX = 0; $TABLE_NAME = basename($EXPORT_FILE,'.sql'); @@ -41,7 +42,16 @@ fwrite($output,'`'.$col_headings[$i]."` VARCHAR(".$cols[$i]."),\n"); } } - fwrite($output,"PRIMARY KEY (`$col_headings[$PK_INDEX]`)\n) DEFAULT CHARACTER SET 'utf8';\n\n"); + + // Write primary key or not + if ($OPTION != "--nopk") { + // trim last comma + fwrite($output); + + fwrite($output,"PRIMARY KEY (`$col_headings[$PK_INDEX]`)\n"); + } + // Write character set + fwrite($output, ") DEFAULT CHARACTER SET 'utf8';\n\n"); // Re-open import file $file = fopen($IMPORT_FILE,'r'); $lineNum = 1; From b8e410e9f66f735c16401516d0e280ba917d4724 Mon Sep 17 00:00:00 2001 From: Jujhar Date: Fri, 17 Oct 2014 14:21:22 -0700 Subject: [PATCH 2/6] Converted readme to Markdown --- README => README.MD | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename README => README.MD (100%) diff --git a/README b/README.MD similarity index 100% rename from README rename to README.MD From a3a97a1384da011449204c5f636d14bf2676b1af Mon Sep 17 00:00:00 2001 From: Jujhar Date: Fri, 17 Oct 2014 15:58:26 -0700 Subject: [PATCH 3/6] Added --nopk option --- README.MD | 14 ++++++++++---- php-csv2mysql.php | 11 +++++++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/README.MD b/README.MD index 3800107..0180e8b 100644 --- a/README.MD +++ b/README.MD @@ -7,9 +7,15 @@ Converts CSV files to .SQL files, with a CREATE TABLE and INSERT statements for Will automatically ignore columns that contain no data, and will make all columns type VARCHAR with a size equal to the length of that column's longest value. ## Instructions -Replace the mysql connect database and password name the applicaion. -to use: php csv_to_sql.php inputfile.csv outputfile.sql database_name +1. Replace the mysql connect database and password name the applicaion. -## Under development -optional commands: --nopk +## To Use +```bash +php csv_to_sql.php inputfile.csv outputfile.sql database_name +``` + +## Optional Commands + +### Disable automatically adding a primary key to first column +--nopk diff --git a/php-csv2mysql.php b/php-csv2mysql.php index 0bcf368..a992a49 100644 --- a/php-csv2mysql.php +++ b/php-csv2mysql.php @@ -39,15 +39,18 @@ fwrite($output,"CREATE TABLE `$DB_NAME`.`$TABLE_NAME` (\n"); for($i = 0; $i< $numCols; $i++){ if( !$unused_cols[$i] ){ - fwrite($output,'`'.$col_headings[$i]."` VARCHAR(".$cols[$i]."),\n"); + if ($i == $numCols-1&&$OPTION == "--nopk") { + fwrite($output,'`'.$col_headings[$i]."` VARCHAR(".$cols[$i].")\n"); + } + else { + fwrite($output,'`'.$col_headings[$i]."` VARCHAR(".$cols[$i]."),\n"); + + } } } // Write primary key or not if ($OPTION != "--nopk") { - // trim last comma - fwrite($output); - fwrite($output,"PRIMARY KEY (`$col_headings[$PK_INDEX]`)\n"); } // Write character set From 46736d5a5626db883df8e9d29acfcbd2d110f922 Mon Sep 17 00:00:00 2001 From: Jujhar Date: Fri, 17 Oct 2014 16:01:33 -0700 Subject: [PATCH 4/6] Update README.MD --- README.MD | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.MD b/README.MD index 0180e8b..9210026 100644 --- a/README.MD +++ b/README.MD @@ -9,7 +9,7 @@ Will automatically ignore columns that contain no data, and will make all column ## Instructions 1. Replace the mysql connect database and password name the applicaion. -## To Use +### To Use ```bash php csv_to_sql.php inputfile.csv outputfile.sql database_name From a4bdfff5e3d40f2da01018b385a20415f07b8bf6 Mon Sep 17 00:00:00 2001 From: Jujhar Date: Fri, 17 Oct 2014 16:02:28 -0700 Subject: [PATCH 5/6] Update README.MD --- README.MD | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.MD b/README.MD index 9210026..9c57158 100644 --- a/README.MD +++ b/README.MD @@ -17,5 +17,5 @@ php csv_to_sql.php inputfile.csv outputfile.sql database_name ## Optional Commands -### Disable automatically adding a primary key to first column +### Disable automatically primary key to first column --nopk From 52100f596646d5aeabb5dbef976f130a77937e91 Mon Sep 17 00:00:00 2001 From: Jujhar Date: Wed, 12 Nov 2014 15:08:17 -0800 Subject: [PATCH 6/6] Update README.MD --- README.MD | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/README.MD b/README.MD index 9c57158..8936188 100644 --- a/README.MD +++ b/README.MD @@ -1,8 +1,13 @@ #php-csv2mysql -Chris Armstrong -chris@chrisarmstrong.me - +**Note on most conditions it is much better to simply import the CSV file using the fast import which will get you done in seconds rather than minutes or hours** + +**Reccomended sytnax with CSV import:** +**_LOAD DATA LOCAL INFILE 'file.csv' INTO TABLE table +FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' +(@col1,@dummy,@col3) set +EX1=@col1,EX2=@col3;_** + Converts CSV files to .SQL files, with a CREATE TABLE and INSERT statements for each row of the CSV. Will automatically ignore columns that contain no data, and will make all columns type VARCHAR with a size equal to the length of that column's longest value.