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

前端 未结 13 1239
感情败类
感情败类 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 10:46

    I just discovered csvkit, which is a set of Unix command-line tools for CSV files. I installed it on my Mac with pip install csvkit. The command was:

    csvsql --dialect mysql --snifflimit 100000 bigdatafile.csv > maketable.sql
    

    You can alternatively provide a DB connection string and it can load the table directly.

    0 讨论(0)
  • 2020-12-04 10:53

    you can use this bash script

    convert.sh

    and run

    ./convert.sh -f example/mycsvfile.csv
    
    0 讨论(0)
  • 2020-12-04 10:54

    "Convert CSV to SQL" helped me. Add your CSV file and you are good to go.

    0 讨论(0)
  • 2020-12-04 10:56

    There is an easier way if you are using phpMyAdmin as your MySQL front end:

    1. Create a database with the default settings.
    2. Select the database.
    3. Click "Import" at the top of the screen.
    4. Select "CSV" under "Format".
    5. Choose the options appropriate to your CSV file, open the CSV file in a text editor and reference it to get the "appropriate" options.

    If you have problems, no problem, simply drop the database and try again.

    0 讨论(0)
  • 2020-12-04 10:56

    MySQL for excel plugin can help you.

    http://dev.mysql.com/doc/refman/5.6/en/mysql-for-excel.html

    Open your CSV file in excel. You can use this plugin to export excel data into a new table of remote or local mysql server. It will analyze your data (top 100 to 1000 rows) and create a corresponding table schema.

    0 讨论(0)
  • 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) );"
    
    0 讨论(0)
提交回复
热议问题