ACCESS資料庫向MySQL快速遷移小程式(一)

來源:互聯網
上載者:User
access|mysql|程式|資料|資料庫 近日,本人為了將為公司開發的一個資訊管理系統從以前試啟動並執行開發機器上(Windows NT + IIS4.0 + Access)遷移至一台真正的Linux伺服器上(Apache1.3.12 + PHP 4.03 + MySQL 3.23.26),其中資料庫中的幾十個表的內容遷移,開始讓我小費了一些周折,從網上也下載了一些MySqL的客戶軟體或是資料庫管理軟體,寫得較好的軟體均有資料移轉功能,但其遷移方式不外乎兩種,一種是採用檔案引入方式,此種方式在處理資料庫中有和分隔字元相同的字元時,會產生錯誤,尤其是在處理ACCESS中的Memo欄位,很容易出錯,最後導致匯出後的資料不是多了就是少了。而另一種支援ODBC直接匯入的功能較強,基本可無錯誤地匯入各個表的內容,但很遺憾,這必須是建立在ACCESS中表格的欄位是英文是才可以,如在ACCESS中欄位是中文名,一般也出錯,不能成功匯入。
為此我只好花了點時間寫了兩個小程式,用於將ACCESS資料庫的內容向MySQL遷移,經使用,效果還不錯,特在此寫出奉獻給各位一試或評判。

先概述一下使用方法,
1,將ACCESS的資料庫建立一個"system DSN";
   2,根據ACCESS資料庫中各表的名稱,在MySQL中建立相應的各個空表;
3,運行fdlist.php;
   4,運行import.php;
   5,每運行一次3,4步可遷移一個表,然後修改fdlist.php中的ACCESS源表名和MySQL中的目標表名,再運行3,4步,直至遷移所有的表,

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
以下為 fdlist.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>
<?  

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

    $odbc_table = "youroriginaltable" ;    // The original table name in your ODBC database

    $mysql_table = "yournewtable" ;        // The new table name in your Mysql Database.


     
?>

<body bgcolor=#f0f0f0 topmargin=0 leftmargin=0 text=#a00000>

<br>
<div style="font-size:24pt;font-family:times;font-weight:bold;color:#00a000">Fields List of Two tables</div>
<hr size=1 color=#900000>

<?

    $conn = odbc_connect("task", "", "");
     
    $odbc_query = "select * from " . $odbc_table . " where 1=2";
     
    $recordsid = odbc_exec($conn, $odbc_query);
     
    $idcounts = odbc_num_fields( $recordsid ) ;

    $fdlist1 = "" ;
     
    for ( $i = 1 ; $i <= $idcounts ; $i ++)  
     
        $fdlist1 .=   odbc_field_name($recordsid,$i)."," ;
     
    echo "<div> Fd1 = " . $fdlist1 ;
     
    $fdlist1 = substr($fdlist1,0,strlen($fdlist1)-1) ;
     
    $fdlist2 = "" ;

         
    $sqlquery = "select * from " . $mysql_table . " where 1=2 " ;  
     
    $records2 = mysql_query ($sqlquery) ;
                 
    $idcount2 = mysql_num_fields ( $records2 ) ;
     
     
     
    for ( $i = 0 ; $i < $idcount2 ; $i++)
     
        $fdlist2 .= mysql_field_name($records2,$i )."," ;
         
    echo "<div> FD2 = " . $fdlist2 ;

    $fdlist2 = substr($fdlist2,0,strlen($fdlist2)-1) ;
         
    $fp = fopen ("fdlist.txt","w") ;
     
    fwrite ($fp,$ctable) ;
     
    fwrite ($fp,"n");
     
    fwrite ($fp,$fdlist1) ;
     
    fwrite ($fp,"n");
     
    fwrite ($fp,$etable) ;
     
    fwrite ($fp,"n") ;
     
    fwrite ($fp,$fdlist2) ;
     
    fclose($fp) ;
         
    odbc_close($conn);

    if ( $idcount2 != $idcounts )  {
     
        echo "<hr size=1 color=#900000>".  
             
            "<div style='font-size:20pt;font-family:times;font-weight:bold'> The fields of two tables doesn't match" ;

        echo "<br><br>ODBC_table Fields = " . $idcounts;

        echo "<br><br>MySQL_table Fields = " . $idcount2;
    }
?>


</body>
</html>

~~~~~~~~~~~~~~~~~~~
未完接(二)
~~~~~~~~~~~~~~~~~~~ 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.