csv导入mysql格式转换,这是CSV文件的最佳格式导入到MySQL数据库

I have data in the following format (sample data, there are many rows):

"Rec Open Date","number 1","number 2","Data Volume (Bytes)","Device Manufacturer","Device Model","Product Description"

"2015-10-06","0427","70060","137765","Samsung Korea","Samsung SM-G900I","$39 option"

"2015-10-06","7592","55620","0","Apple Inc","Apple iPhone 6 (A1586)","some text #16"

...

what I want to know is, what is the best format/practice for importing this into mysql?

Some specific questions are:

Should the date be "2015-10-06"

Should columns 2, 3, and 4 be in string format with double quotes e.g. "0427"

For the column headers, should I remove all the spaces and the brackets

Anything else

maybe my data would be better looking like this before importing it into my database:

Replace all spaces with underscore

remove brackets

turn columns 2, 3, and 4 into values by removing the double quotes

which would look like this:

"Rec_Open_Date","number_1","number_2","Data_Volume_Bytes","Device_Manufacturer","Device_Model","Product_Description"

"2015-10-06",0427,70060,137765,"Samsung Korea","Samsung SM-G900I","$39 option"

"2015-10-06",7592,55620,0,"Apple Inc","Apple iPhone 6 (A1586)","some text #16"

...

Again just looking for best practice out there.

The next question will be is there a parser that can do all this, maybe in bash or other equivalent?

解决方案

1.One way to do this is by importing the csv file into mysql, you can use tools like phpMyAdmin.

2.You can try this code. Its a php script to convert the file to mysql.

$databasehost = "localhost";

$databasename = "test";

$databasetable = "sample";

$databaseusername="test";

$databasepassword = "";

$fieldseparator = ",";

$lineseparator = "\n";

$csvfile = "filename.csv";

if(!file_exists($csvfile)) {

die("File not found. Make sure you specified the correct path.");

}

try {

$pdo = new PDO("mysql:host=$databasehost;dbname=$databasename",

$databaseusername, $databasepassword,

array(

PDO::MYSQL_ATTR_LOCAL_INFILE => true,

PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION

)

);

} catch (PDOException $e) {

die("database connection failed: ".$e->getMessage());

}

$affectedRows = $pdo->exec('

LOAD DATA LOCAL INFILE ".$pdo->quote($csvfile)." INTO TABLE `$databasetable`

FIELDS TERMINATED BY ".$pdo->quote($fieldseparator)."

LINES TERMINATED BY ".$pdo->quote($lineseparator))." ');

echo "Loaded a total of $affectedRows records from this csv file.\n";

?>

3.You can use any framework or tool to do so.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值