如何将数据从文本导入到mysql


access中可以将文本中的数据轻松导入表中,mysql中用起来没那么方便,其实起来也很简单。
首先将数据记录按行处理好用特定的字符分开如:“,”
记录形如:
aaa,bbb,ccc,ddd,eee
fff,ggg,hhh,iii,jjj,kkk
就行,建立loaddate.php

 1   
 2$hostname="localhost";   
 3$username="yourname";   
 4$password="yourpwd";   
 5$dbname="yourdb";   
 6mysql_connect($hostname,$username,$password);   
 7mysql_select_db("$dbname");   
 8$mydate=file("yourdate.txt");   
 9$n=count($mydate);   
10for($i=0;$i<$n;$i++){   
11$date=explode(",",$mydate[$i]);   
12$str="insert into ip values('$date[0]','$date[1]','$date[2]','$date[3]','$date[4]')";//   
13mysql_query($str);   
14}   
15mysql_close();   
16echo "ok!";   

运行loaddate.

Published At
Categories with 数据库类
comments powered by Disqus