5

Automatically build mySql table upon a CSV file upload.

I have a admin section where admin can upload CSV files with different column count and different column name.


which it should then build a mySql table in the db which will read the first line and create the columns and then import the data accordingly.

I am aware of a similar issue, but this is different because of the following specs.

  • The name of the Table should be the name of the file (minus the extension [.csv])
  • each csv file can be diffrent
  • Should build a table with number of columns and names from the CSV file
  • add the the data from the second line and on

Here is a design sketch

Maybe there are known frameworks that makes this easy. Thanks.

2
  • Why do you want this? How are you going to use the data? Commented Jun 22, 2011 at 19:52
  • @Sjoerd, i updated the question and added a design sketch, hope that helps. Commented Jun 22, 2011 at 20:09

5 Answers 5

8
$file = 'filename.csv';
$table = 'table_name';

// get structure from csv and insert db
ini_set('auto_detect_line_endings',TRUE);
$handle = fopen($file,'r');
// first row, structure
if ( ($data = fgetcsv($handle) ) === FALSE ) {
    echo "Cannot read from csv $file";die();
}
$fields = array();
$field_count = 0;
for($i=0;$i<count($data); $i++) {
    $f = strtolower(trim($data[$i]));
    if ($f) {
        // normalize the field name, strip to 20 chars if too long
        $f = substr(preg_replace ('/[^0-9a-z]/', '_', $f), 0, 20);
        $field_count++;
        $fields[] = $f.' VARCHAR(50)';
    }
}

$sql = "CREATE TABLE $table (" . implode(', ', $fields) . ')';
echo $sql . "<br /><br />";
// $db->query($sql);
while ( ($data = fgetcsv($handle) ) !== FALSE ) {
    $fields = array();
    for($i=0;$i<$field_count; $i++) {
        $fields[] = '\''.addslashes($data[$i]).'\'';
    }
    $sql = "Insert into $table values(" . implode(', ', $fields) . ')';
    echo $sql; 
    // $db->query($sql);
}
fclose($handle);
ini_set('auto_detect_line_endings',FALSE);
Sign up to request clarification or add additional context in comments.

Comments

4

Maybe this function will help you.

fgetcsv

(PHP 4, PHP 5)

fgetcsv — Gets line from file pointer and parse for CSV fields

http://php.net/manual/en/function.fgetcsv.php

Comments

3

http://bytes.com/topic/mysql/answers/746696-create-mysql-table-field-headings-line-csv-file has a good example of how to do this.

The second example should put you on the right track, there isn't some automatic way to do it so your going to need to do a lil programming but it shouldn't be too hard once you implement that code as a starting point.

Comments

1

Building a table is a query like any other and theoretically you could get the names of your columns from the first row of a csv file.

However, there are some practical problems:

  • How would you know what data type a certain column is?
  • How would you know what the indexes are?
  • How would you get data out of the table / how would you know what column represents what?

As you can´t relate your new table to anything else, you are kind of defeating the purpose of a relational database so you might as well just keep and use the csv file.

1 Comment

All column's will have the VARCHAR data-type, I hope the sketch i uploaded will help u understand the question better.
1

What you are describing sounds like an ETL tool. Perhaps Google for MySQL ETL tools...You are going to have to decide what OS and style you want.

Or just write your own...

1 Comment

mclintock Thanks, Please see updated question, where i added a design sketch, hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.