简体   繁体   中英

Importing a CSV file into mysql. (Specifically about create table command)

I hava text file full of values like this:

The first line is a list of column names like this:

col_name_1, col_name_2, col_name_3 ......(600 columns)

and all the following columns have values like this:

1101,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,1101,1,3.86,65,0.46418,65,0.57151...

What is the best way to import this into mysql?

Specifically how to come up with the proper CREATE TABLE command so that the data will load itself properly? What is the best generic data type which would take in all the above values like 1101 or 3.86 or 0.57151. I am not worried about the table being inefficient in terms of storage as I need this for a one time usage.

I have tried some of the suggestions in other related questions like using Phpmyadmin (it crashes I am guessing due to the large amount of data)

Please help!

Data in CSV files is not normalized; those 600 columns may be spread across a couple of related tables. This is the recommended way of treating those data. You can then use fgetcsv() to read CSV files line-by-line in PHP.

To make MySQL process the CSV, you can create a 600 column table (I think) and issue a LOAD DATA LOCAL INFILE statement (or perhaps use mysqlimport , not sure about that).

The most generic data type would have to be VARCHAR or TEXT for bigger values, but of course you would lose semantics when used on numbers, dates, etc.

I noticed that you included the phpmyadmin tag.

PHPMyAdmin can handle this out of box. It will decide "magically" which types to make each column, and will CREATE the table for you, as well as INSERT all the data. There is no need to worry about LOAD DATA FROM INFILE , though that method can be more safe if you want to know exactly what's going on without relying on PHPMyAdmin's magic tooling.

尝试convertcsvtomysql ,仅上传您的csv文件,然后您可以下载和/或复制mysql语句以创建表并插入行。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM