php读取含有3w条以上数据的csv文件,并选择性将相应字段的数据导入至mysql

1.数据库建议好数据表,确认具有哪些字段和字段对应的类型;
2.连接数据库的页面(database.php)
<?php
$Server="localhost";
$User="root";
$pw="****";//密码
$db="****";//数据库名
$con=mysqli_connect($Server,$User,$pw,$db);
// 检查连接
if (!$con)
{
die("连接错误: " . mysqli_connect_error());
}
else
echo "数据库连接成功"."<br>";
//设置客户端和连接字符集
mysqli_set_charset($con,"utf8");
?>


3.前端上传csv文件的页面(load.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>

<form action='load.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file">
<input type="submit" name="submit" value="确认上传文件" >
</form>
</body>
</html>

4.后端处理上传文件并导入至数据库的页面(load.php)
<?php
header("content-type:text/html;charset=utf-8");
include("database.php");
echo "亲,文件过大,导入数据库需要一会儿,请耐心等候!";
$n=$_FILES["file"]["tmp_name"];
$row = 0;//行指标
$result=1;//最后根据此值查看数据是否全部导入
if (($handle = fopen("$n", "r")) !== FALSE)
{
$sql="insert into user(company_name,saler,sector,last_time) values('";
while (( $data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$flag=1;//用于查看第15列的值是否为空,若为空此行就不执行sql语句
$row++;
if ($row == 1)
continue;
$num = count($data);
// 这里会依次输出每行当中每个单元格的数据
for ($i=0; $i<$num; $i++)//字段标号是从0(对应A列)开始的。
{
$Name=iconv('gb2312','utf-8',$data[$i]);
if($i==1||$i==2||$i==3||$i==15)//只选取1,2,3,15列的数据插入到数据库
{
if($i==15)
{
if($Name==null)
{
$sql=$sql.'null'."')";$flag=0;break;//last_time为空,就不执行此sql语句
}
else
{
$sql=$sql.$Name."')";break;//到第15列后,后面列的数据都不需要插入,所以sql语句填写完毕;
}
}
else{
$sql=$sql.$Name."','";
}

}
else
{
continue;
}
}
if($flag==0)//第15列字段值为空,不执行sql语句
{
$sql="insert into user(company_name,saler,sector,last_time) values('";
continue;//跳出此次循环
}
$line=$row-1;//真正的行号,用于提示第几行数据插入情况
if(!mysqli_query($con,$sql))
{
echo "第".$line."行插入数据库失败";
$result=0;
break;
}
else
{
echo "第".$line."行插入数据库成功"."<br>";
}
$sql="insert into user(company_name,saler,sector,last_time) values('";
}
}
fclose($handle);
if($result==1)//数据全部导入成功
{
echo"<script>alert('文件已成功导入数据库!');</script>";
echo"<script>location.href='account.html'</script>";
exit;
}
else
{
echo"<script>alert('文件数据导入失败');</script>";
echo"<script>location.href='account.html'</script>";
exit;
}
?>
5.结果


缺点:运行有点慢,我后期看下是否有更快的方法。

亲测有效。有何疑问可以私我。(要敢于挑战自己,遇到问题不要退缩。)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Python编写一个脚本来实现这个功能。 首先,需要安装Python的MySQL驱动程序,可以通过以下命令安装: ``` pip install mysql-connector-python ``` 然后,可以使用Python的csv模块来读取CSV文件,例如: ```python import csv with open('data.csv', 'r') as file: reader = csv.reader(file) for row in reader: print(row) ``` 这将打开名为data.csv文件,并将其内容打印到控制台。 接下来,需要连接到MySQL数据库并将数据插入其中。可以使用Python的mysql.connector模块来实现这一点,例如: ```python import mysql.connector mydb = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword", database="mydatabase" ) mycursor = mydb.cursor() sql = "INSERT INTO customers (name, address) VALUES (%s, %s)" val = ("John", "Highway 21") mycursor.execute(sql, val) mydb.commit() print(mycursor.rowcount, "record inserted.") ``` 这将连接到MySQL数据库,并将名为"John"的客户插入到名为"customers"的表中。 最后,将读取CSV文件和将数据插入到MySQL数据库结合起来,可以编写一个完整的Python脚本来实现这个功能,例如: ```python import csv import mysql.connector mydb = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword", database="mydatabase" ) mycursor = mydb.cursor() with open('data.csv', 'r') as file: reader = csv.reader(file) for row in reader: sql = "INSERT INTO customers (name, address) VALUES (%s, %s)" val = (row[0], row[1]) mycursor.execute(sql, val) mydb.commit() print(mycursor.rowcount, "records inserted.") ``` 这将读取名为data.csv文件,并将其内容插入到名为"customers"的表中。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值