csv文件上传mysql,将大型CSV文件上传到Mysql数据库

I want to upload large CSV document into mysql database can anyone help me out, the table consist of 10 fields but i want to upload into only 5 fields of the table without a heading. I want this done with php in the browser

$filename = $_FILES['sel_file']['tmp_name'];

if($_FILES["file"]["type"] != "application/vnd.ms-excel"){

die("This is not a CSV file.");

}

elseif(is_uploaded_file($_FILES['file']['name'])){

$dbhost = 'localhost';

$dbuser = 'root';

$dbpass = '';

$dbname = 'cbt_software';

$link = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql server');

mysql_select_db('cbt_software') or die(mysql_error());

//Process the CSV file

$handle = fopen($_FILES['file']['name'], "r");

$data = fgetcsv($handle, 1000, ";");

while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {

$att0 = mysql_real_escape_string($data[0]);

$att1 = mysql_real_escape_string($data[1]);

$att2 = mysql_real_escape_string($data[2]);

$att3 = mysql_real_escape_string($data[3]);

$att4 = mysql_real_escape_string($data[4]);

$sql = "INSERT INTO `course_reg` (`coursecode`,`coursename`,`coursedescription`,`coursemaster`,`courselevel`)VALUES ('$att0','$att1','$att2','$att3','$att4')";

mysql_query($sql) or die(mysql_error());

}

mysql_close($link);

echo "CSV file successfully imported.";

}

else{

die("You shouldn't be here");

}

?>

At first this imported all the field from the csv into just one field in the database and after i tampered with the code it is not recognicing it a s a CSV file.

解决方案

If you have the appropriate permissions, you can do so directly in MySQL with the LOAD DATA INFILE command, see http://dev.mysql.com/doc/refman/4.1/en/load-data.html or the mysqlimport utility, see http://dev.mysql.com/doc/refman/4.1/en/mysqlimport.html

Both methods will allow you to specify which columns the data should go in, for instance:

LOAD DATA INFILE 'myfile.txt' INTO TABLE 'mytable' (col1, col2, col3, ...)

or

mysqlimport --columns='col1,col2,...' tablename.csv

If you intend to do it from PHP, you should be able to read each line of the CSV file and execute an appropriate SQL INSERT query naming the appropriate columns (although that will not be as efficient as doing it directly in MySQL).

EDIT: I should add that you haven't mentioned what you've tried so far or what you're finding difficult; if you're stuck on something in particular, rather than just looking for suggestions on how to go about doing it, please update the question to say so.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Python编写一个脚本来自动将上传到FTP目录的csv文件导入MySQL数据库。以下是主要步骤: 1. 首先,需要安装ftplib库和pymysql库来连接FTP和MySQL数据库。 2. 连接FTP服务器,使用ftplib库下载csv文件到本地。 3. 使用pandas库读取csv文件,将数据转换为DataFrame格式。 4. 使用pymysql库连接MySQL数据库,并将DataFrame数据导入到MySQL数据库中。 5. 最后,删除FTP服务器上已经导入到MySQL数据库中的csv文件。 以下是一个Python脚本的示例代码: ```python import ftplib import os import pandas as pd import pymysql # FTP服务器信息 ftp_server = "ftp.example.com" ftp_username = "username" ftp_password = "password" ftp_remote_path = "/csv_files/" # MySQL数据库信息 mysql_host = "localhost" mysql_port = 3306 mysql_username = "root" mysql_password = "password" mysql_database = "database_name" mysql_table = "table_name" # 连接FTP服务器并下载文件 ftp = ftplib.FTP(ftp_server) ftp.login(ftp_username, ftp_password) ftp.cwd(ftp_remote_path) file_list = ftp.nlst() for file_name in file_list: if file_name.endswith(".csv"): local_file_path = os.path.join(os.getcwd(), file_name) with open(local_file_path, "wb") as local_file: ftp.retrbinary("RETR " + file_name, local_file.write) # 将csv文件导入到MySQL数据库中 df = pd.read_csv(local_file_path) conn = pymysql.connect(host=mysql_host, port=mysql_port, user=mysql_username, password=mysql_password, database=mysql_database) cursor = conn.cursor() for index, row in df.iterrows(): sql = f"INSERT INTO {mysql_table} (col1, col2, col3) VALUES ('{row['col1']}', '{row['col2']}', '{row['col3']}')" cursor.execute(sql) conn.commit() cursor.close() conn.close() # 删除FTP服务器上已经导入到MySQL数据库中的csv文件 ftp.delete(file_name) os.remove(local_file_path) ftp.quit() ``` 其中,需要根据实际情况修改FTP服务器信息、MySQL数据库信息和CSV文件的列名和数据类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值