หน้าเว็บ

วันอาทิตย์ที่ 2 กันยายน พ.ศ. 2555

ทบทวน .csv import to mySQL

บทความแปล
โดย อิสระพงษ์ สิทธิพล

Google keywords: php csv import into mysql

PHP script to import csv data into mysql

This is a simple script that will allow you to import csv data into your database. This comes handy because you can simply edit the appropriate fields, upload it along with the csv file and call it from the web and it will do the rest.
It allows you to specify the delimiter in this csv file, whether it is a coma, a tab etc. It also allows you to chose the line separator, allows you to save the output to a file (known as a data sql dump).
It also permits you to include an empty field at the beginning of each row, which is usually an auto increment integer primary key.
This script is useful mainly if you don’t have phpmyadmin, or you don’t want the hassle of logging in and prefer a few clicks solution, or you simply are a command prompt guy.
Just make sure the table is already created before trying to dump the data.

แปล: Script PHP สำหรับนำเข้า csv ไปยัง mySQL

นี่เป็นตัวอย่าง script ง่ายๆ ที่ใช้นำเข้าข้อมูลจากไฟล์ .csv ไปยังฐานข้อมูล MySQL ซึ่งเป็นประโยชน์มากเพราะสามารถแก้ไข field ต่างๆ ที่สอดคล้องกับ DB ของเราได้ง่าย upload ไฟล์ csv ขึ้นไปแล้วเรียกจากใช้ผ่านเว็บ (คงประมาณว่า copy - paste URL ของไฟล์) แล้วที่เหลือ script จะจัดการต่อเอง

ท่านยังสามารถกำหนดสัญลักษณ์ที่ใช้แบ่ง field ใน csv ได้ด้วย ไม่ว่าจะเป็นคอมม่า "," หรือแทป "<Tab>" เป็นต้น และยังสามารถเลือกตัวแย่งบรรทัด หรือเลือกบันทึกไปยังไฟล์ในรูปคำสั่งของ sql  (sql dump) ได้

มันยังอนุญาตให้ field แรกว่างได้ในแต่ละแถว เพื่อทำ auto increment สำหรับ pk ให้
 script นี้เป็นประโยชน์นอกจากท่านจะใช้ phpmyadmin เอาก็ได้ หรือท่านไม่ต้องการจะยุ่งยากกับการ login และทำงานเพียงไม่กี่คลิก หรือท่านเป็นคนที่ถนัดกับการใช้ command prompt ก็หันมาใช้ script นี้ได้

แค่มีตารางพร้อมก่อนที่จะ dump ข้อมูล

Download ไฟล์ได้ที่นี่

ไม่มีความคิดเห็น:

แสดงความคิดเห็น