Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I keep getting these errors when I create MySQL tables from CSV files.

SQL
ERROR
 - Error No: 1103
 - Error: Incorrect table name 'Hospital_Value_Based_Purchasing__HVBP____Healthcare_Associated_Infection_Scores'
 - Call: Function createTableFromCSV in /home/dojob/public_html/drydo.com/csv_to_mysql/csv_to_mysql.php on line 46
ERROR
 - Error No: 0
 - Error:
 - Call: Function createTableFromCSV in /home/dojob/public_html/drydo.com/csv_to_mysql/csv_to_mysql.php on line 46


Script that creates the tables

PHP
require "MySQL_wrapper.class.php";

$filename = 'csv_list.txt';
$contents = file($filename);


/* Loops through list of CSV-files and create a table for each CSV-file */
foreach($contents as $line) {

    /* Cuts off file extension ".csv"*/
    $file_info = pathinfo($line);
    $file_name = $file_info['filename'];


    
/*Replace "-" with "_" to be readable for server */
    $file_name = preg_replace('/-/','_',$file_name);


There's more Errors when preg_replace is used. I get 15 tables into the database when not using preg_replace and I get 4 tables when I'm using preg_replace.

PHP
    $db->dropTable($file_name);
    $db->createTableFromCSV('../flashback_crawler/files/'.$file_name.'.csv', $file_name);
}



Method createTableFromCSV

PHP
/**Creates table from CSV file and imports CSV data to Table with possibility to update rows while import.
 * @param   string      $file           - CSV File path
 * @param   string      $table          - Table name
 * @param   string      $delimiter      - COLUMNS TERMINATED BY (Default: ',')
 * @param   string      $enclosure      - OPTIONALLY ENCLOSED BY (Default: '"')
 * @param   string      $escape         - ESCAPED BY (Default: '\')
 * @param   integer     $ignore         - Number of ignored rows (Default: 1)
 * @param   array       $update         - If row fields needed to be updated eg date format or increment (SQL format only @FIELD is         variable with content of that field in CSV row) $update = array('SOME_DATE' => 'STR_TO_DATE(@SOME_DATE, "%d/%m/%Y")', 'SOME_INCREMENT' => '@SOME_INCREMENT + 1')
 * @param   string      $getColumnsFrom - Get Columns Names from (file or generate) - this is important if there is update while inserting (Default: file)
 * @param   string      $newLine        - New line delimiter (Default: auto detection use \n, \r\n ...)
 * @return  number of inserted rows or false
 */
Posted

1 solution

The maximum length for table name and most other identifiers is 64 characters: https://dev.mysql.com/doc/refman/5.7/en/identifiers.html[^]
The name you are trying to use is 79 characters long. Add code to limit the length of identifiers.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900