Create mysql table directly from CSV file using the CSV Storage engine?

前端 未结 13 1330
感情败类
感情败类 2020-12-04 10:08

I just learned that MySQL has a native CSV storage engine which stores data in a Comma-Separated-Value file per table.

Is it possible to create a table directly from

13条回答
  •  借酒劲吻你
    2020-12-04 11:02

    This is not possible. To create a table you need a table schema. What you have is a data file. A schema cannot be created with it.

    What you can do is check if your file has a header row, and, in that case, you can manually create a table using that header row.

    However, there is a way to generate a create table statement using a batch file as described by John Swapceinski in the comment section of the MySQL manual.

    Posted by John Swapceinski on September 5 2011 5:33am.
    Create a table using the .csv file's header:

    #!/bin/sh
    # pass in the file name as an argument: ./mktable filename.csv
    echo "create table $1 ( "
    head -1 $1 | sed -e 's/,/ varchar(255),\n/g'
    echo " varchar(255) );"
    

提交回复
热议问题