⽤java把csv导⼊mysql_将CSV导⼊mysql表
回答(12)
2 years ago
您可以直接将MYSQL链接到它并使⽤以下SQL语法上载信息,⽽不是编写脚本以从CSV⽂件中提取信息 .
要将Excel⽂件导⼊MySQL,请先将其导出为CSV⽂件 . 从⽣成的CSV⽂件中删除CSV Headers 以及Excel可能放在CSV⽂件末尾的空数据 .
然后,您可以通过运⾏以下命令将其导⼊MySQL表:
load data local infile 'uniq.csv' into table tblUniq fields terminated by ','
enclosed by '"'
lines terminated by '\n'
(uniqName, uniqCity, uniqComments)
编辑
对于您的情况,您需要先编写⼀个解释器,以查第⼀⾏,并将它们指定为列名 .
编辑-2
IGNORE数字LINES选项可⽤于忽略⽂件开头的⾏ . 例如,您可以使⽤IGNORE 1 LINES跳过包含列名的初始 Headers ⾏:LOAD DATA INFILE'/ tmp / 'INTO TABLE test IGNORE 1 LINES;
因此,您可以使⽤以下语句:
LOAD DATA LOCAL INFILE 'uniq.csv'
INTO TABLE tblUniq
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(uniqName, uniqCity, uniqComments)
2 years ago
这是⼀个简单的PHP命令⾏脚本,可以满⾜您的需求:
$host = 'localhost';
$user = 'root';
$pass = '';
$database = 'database';
$db = mysql_connect($host, $user, $pass);
mysql_query("use $database", $db);
/********************************************************************************/
// Parameters: filename.csv table_name
$argv = $_SERVER[argv];
if($argv[1]) { $file = $argv[1]; }
else {
echo "Please provide a file name\n"; exit;
}
if($argv[2]) { $table = $argv[2]; }
else {
$table = pathinfo($file);
$table = $table['filename'];
}
/********************************************************************************/
// Get the first row to create the column headings
$fp = fopen($file, 'r');
$frow = fgetcsv($fp);
foreach($frow as $column) {
if($columns) $columns .= ', ';
$columns .= "`$column` varchar(250)";
}
$create = "create table if not exists $table ($columns);";
mysql_query($create, $db);
/
********************************************************************************/
// Import the data into the newly created table.
$file = $_SERVER['PWD'].'/'.$file;
$q = "load data infile '$file' into table $table fields terminated by ',' ignore 1 lines"; mysql_query($q, $db);
>
它将基于第⼀⾏创建⼀个表,并将剩余的⾏导⼊其中 . 这是命令⾏语法:
php csv_import.php csv_file.csv table_name
2 years ago
如果你有能⼒安装phpadmin有⼀个导⼊部分,你可以在其中导⼊csv⽂件到你的数据库甚⾄有⼀个复选
框,设置 Headers 到⽂件的第⼀⾏包含表列名称(如果未选中,则第⼀⾏将成为数据的⼀部分
2 years ago
⾸先在数据库中创建⼀个表,其中包含csv⽂件中相同数量的列 .
然后使⽤以下查询
LOAD DATA INFILE 'D:/Projects/testImport.csv' INTO TABLE cardinfo
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
2 years ago
要从⽂本⽂件或csv⽂件加载数据,命令是
load data local infile 'file-name.csv'
into table table-name
fields terminated by '' enclosed by '' lines terminated by '\n' (column-name);
在上⾯的命令中,在我的情况下,只有⼀列要加载,所以没有“终⽌”和“括号”所以我保持空,否则程序员可以输⼊分隔字符 . 例如 . ,(逗号)或“或;或任何事物 .
适⽤于使⽤mysql 5及以上版本的⽤户
在将⽂件加载到mysql之前,必须确保在下⾯添加拖曳线 etc/mysql/myf
编辑myf命令是
sudo vi /etc/mysql/myf
[mysqld]
local-infile
[mysql]
local-infile
2 years ago
如果你启动mysql为“mysql -u -p --local-infile”,它将正常⼯作
2 years ago
我摔跤了⼀段时间 . 问题不在于如何加载数据,⽽在于如何构造表来保存数据 . 在导⼊数据之前,必须⽣成DDL语句来构建表 .如果表具有⼤量列,则特别困难 .
这是⼀个(⼏乎)完成⼯作的python脚本:
#!/usr/bin/python
import sys
import csv
# get file name (and hence table name) from command line
# exit with usage if no suitable argument
if len(sys.argv) < 2:
ifile = sys.argv[1]
# emit the standard invocation
print 'create table ' + ifile + ' ('
with open(ifile + '.csv') as inputfile:
reader = csv.DictReader(inputfile)
for row in reader:
k = row.keys()
for item in k:
print '`' + item + '` TEXT,'
break
print ')\n'
它要解决的问题是最终的字段名称和数据类型声明以逗号结束,⽽mySQL解析器不会容忍它 .
当然它也存在问题,它为每个字段使⽤TEXT数据类型 . 如果表有⼏百列,那么VARCHAR(64)将使表太⼤ .
这似乎也打破了mySQL的最⼤列数 . 那是时候转移到Hive或HBase了,如果你能的话 .
2 years ago
我写了⼀些代码来做到这⼀点,我将提出⼏个⽚段:
$dir = getcwd(); // Get current working directory where this .php script lives
$fileList = scandir($dir); // scan the directory where this .php lives and make array of file names 然后获取CSV标头,以便告诉mysql如何导⼊(注意:确保您的mysql列与csv列完全匹配):
//extract headers from .csv for use in import command
$headers = str_replace("\"", "`", array_shift(file($path)));
$headers = str_replace("\n", "", $headers);国外java php
然后将您的查询发送到mysql服务器:
mysqli_query($cons, '
LOAD DATA LOCAL INFILE "'.$path.'"
INTO TABLE '.$dbTable.'
FIELDS TERMINATED by \',\' ENCLOSED BY \'"\'
LINES TERMINATED BY \'\n\'
IGNORE 1 LINES
('.$headers.')
;
')or die(mysql_error());
2 years ago
这是我在Python中使⽤csv和MySQL Connector的⽅式:
import csv
tor
credentials = dict(user='...', password='...', database='...', host='...')
connection = t(**credentials)
cursor = connection.cursor(prepared=True)
stream = open('filename.csv', 'rb')
csv_file = csv.DictReader(stream, skipinitialspace=True)
query = 'CREATE TABLE t ('
query += ','.join('`{}` VARCHAR(255)'.format(column) for column in csv_file.fieldnames)
query += ')'
for row in csv_file:
query = 'INSERT INTO t SET '
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论