发新话题
打印

[转载]将ACCESS数据库转填充到MySQL

[转载]将ACCESS数据库转填充到MySQL

信息来源:http://ziqi.512j.com/

以下为 import.php 源程序
<html>
<head>
<style type=text/css>
body,td,li,div,p,pre,a,b,h1,h2,h3,h4 { font-family:verdana;font-size:9pt;line-height : 18px;color:#a00000 }  
</style>
</head>
<body bgcolor=#f0f0f0 topmargin=0 leftmargin=0 text=#a00000>
<center>
<br>
<div style="font-size:24pt;font-family:times;font-weight:bold;color:#008000">ODBC --> MySQL Migrant</div>

<hr size=1 color=#900000>

<?  

  $dbconnection = @mysql_connect("yourmysqlserver", "mysqlaccount", "mysqlpassword")  
   
     or die ("can not connect to database server");
   
  @mysql_select_db("yourdatabase")  
         
        or die("<p style=&#39;font-size:9pt;font-family:verdana;color:#803333;font-weight:bold&#39;>No Database,</p>") ;  


  $conn = odbc_connect("task", "", "");
   
  $fp = fopen ("fdlist.txt","r") ;
   
  $table1 = fgets($fp,200);

  $fd1 = fgets($fp,1024) ;

  $table2 = fgets($fp,200);

  $fd2 = fgets($fp,1024) ;
   
   
  $query1 =  "select " . $fd1 . " from " . $table1 ;  

  $query2 =  "select " . $fd2 . " from " . $table2 . " where 1=2 " ;
   
  $result = mysql_query ($query2) ;
   
  mysql_query ("delete from " .$table2 ) ;
   
  echo "sql=". $query1;
   
  $recordsid = odbc_exec($conn, $query1);
   
  $idcounts = odbc_num_fields( $recordsid ) ;
   
  $idcount2 = mysql_num_fields($result) ;
   
  if ( $idcounts != $idcount2 )  
     die (" The fields of two tables doesn&#39;t match ") ;

  echo "<table width=90% border=1 bordercolorlight=#000000 bordercolordark=#ffffff cellpadding=3 cellspacing=0>n" ;
   
   

  echo  "<tr align=center><td> n " ;
   
  for ( $i = 1 ; $i <= $idcounts ; $i ++)  
   
     echo  "n<td>" . odbc_field_name($recordsid,$i) ;
   
  $theno = 1 ;
   
  echo  "<tr>n" ;
   
   
   
  while (odbc_fetch_row($recordsid) )  {
   
      
      
     $runsql = "insert into " . $table2 . "(" . $fd2 . ") values (" ;
      
     for ( $i = 1 ; $i <= $idcounts ; $i ++) {
      
        $fdv = odbc_result($recordsid,$i) ;  
        
        
        if ( mysql_field_type($result,$i-1) == "string")
        
          $runsql .= "&#39;". $fdv . "&#39;," ;
        else
          $runsql .= $fdv. "," ;
           
      }

     $runsql = substr($runsql,0,strlen($runsql)-1) ;

     $runsql .= ")" ;

     mysql_query ($runsql) ;

     $theno++ ;
   }
   

  echo "Total Convert : " . $theno -- ;      

   
   
  odbc_close($conn);

?>

</body>
</html>
有经验的网友可能看了两个小程序,可能觉得如在ACCESS数据库
中各表的字段是英文的话,fdlist.php有点多余,其实在这里,我的
本意是在迁移时,可以很方便地删除某些不用的字段,只要将fdlist.php的生成文件fdlist.txt中对应的字段Delete掉即可。还算是很方便的,反正,有了这两个小程序,我以前放在ACCESS中的几十个表格的数据,二十分钟全部迁移完毕,包括对其中四、五个表格字段的修改。

TOP

发新话题