使用CSV存储引擎直接从CSV文件创建mysql表? [英] Create mysql table directly from CSV file using the CSV Storage engine?

查看:497
本文介绍了使用CSV存储引擎直接从CSV文件创建mysql表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚了解到,Mysql有一个本机 CSV存储引擎,它以每个表的逗号分隔值文件存储数据。



可以直接从上传的CSV文件创建表,例如: / p>

CREATE TABLE USERS< PATH / USERS.CSV



其中users.csv由用户上传?

解决方案

这是不可能的。要创建表,您需要一个表模式。你有一个数据文件。无法使用它创建模式。你可以做的是检查你的文件是否有标题行。在这种情况下,您可以使用该标题行但是手动创建表。



但是有一种方法可以通过批处理文件生成create table语句,如 John Swapceinski在MySQL手册的注释部分


由John Swapceinski发表于2011年9月5日上午5:33。

使用.csv文件头创建表:




 #!/ bin / sh 
#作为参数传递文件名: ./mktable filename.csv
echocreate table $ 1(
head -1 $ 1 | sed -e's /,/ varchar(255),\\\
/ g'
echo varchar(255));


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 a uploaded CSV file, something like:

CREATE TABLE USERS < PATH/USERS.CSV

where users.csv is uploaded by the user?

解决方案

This is not possible. To create table you need a table schema. What you have is a data file. Schema cannot be created with it. What you can do is check if your file has header row. In that case you can create table using that header row but manually.

However there is a way to generate create table statement by a batch file as described by John Swapceinski in the comment section of 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) );"

这篇关于使用CSV存储引擎直接从CSV文件创建mysql表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆