近日,本人为了将为公司开发的一个信息管理系统从以前试运行的开发机器上(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步,直至迁移所有的表,
1以下为 fdlist.php源程序
1<html>
2<head>
3<style type="text/css">
4body,td,li,div,p,pre,a,b,h1,h2,h3,h4 {font-family:verdana;font-size:9pt;line-height : 18px;color:#a00000 }
5</style>
6</head>
7<?
8
9$dbconnection = @mysql_connect("yourmysqlserver", "mysqlaccount", "mysqlpassword")
10
11or die ("can not connect to database server");
12
13@mysql_select_db("yourdatabase")
14
15or die("<p style='font-size:9pt;font-family:verdana;color:#803333;font-weight:bold'>No Database,") ;
16
17$odbc_table = "youroriginaltable" ; // The original table name in your ODBC database
18
19$mysql_table = "yournewtable" ; // The new table name in your Mysql Database.
20
21
22
23?>
24
25<body bgcolor="#f0f0f0" leftmargin="0" text="#a00000" topmargin="0">
26<br/>
27<div style="font-size:24pt;font-family:times;font-weight:bold;color:#00a000">Fields List of Two tables</div>
28<hr color="#900000" size="1"/>
29<?
30
31$conn = odbc_connect("task", "", "");
32
33$odbc_query = "select * from " . $odbc_table . " where 1=2";
34
35$recordsid = odbc_exec($conn, $odbc_query);
36
37$idcounts = odbc_num_fields( $recordsid ) ;
38
39$fdlist1 = "" ;
40
41for ( $i = 1 ; $i <= $idcounts ; $i ++)
42
43$fdlist1 .= odbc_field_name($recordsid,$i)."," ;
44
45echo "<div> Fd1 = " . $fdlist1 ;
46
47$fdlist1 = substr($fdlist1,0,strlen($fdlist1)-1) ;
48
49$fdlist2 = "" ;
50
51
52$sqlquery = "select * from " . $mysql_table . " where 1=2 " ;
53
54$records2 = mysql_query ($sqlquery) ;
55
56$idcount2 = mysql_num_fields ( $records2 ) ;
57
58
59
60for ( $i = 0 ; $i < $idcount2 ; $i++)
61
62$fdlist2 .= mysql_field_name($records2,$i )."," ;
63
64echo "<div> FD2 = " . $fdlist2 ;
65
66$fdlist2 = substr($fdlist2,0,strlen($fdlist2)-1) ;
67
68$fp = fopen ("fdlist.txt","w") ;
69
70fwrite ($fp,$ctable) ;
71
72fwrite ($fp,"n");
73
74fwrite ($fp,$fdlist1) ;
75
76fwrite ($fp,"n");
77
78fwrite ($fp,$etable) ;
79
80fwrite ($fp,"n") ;
81
82fwrite ($fp,$fdlist2) ;
83
84fclose($fp) ;
85
86odbc_close($conn);
87
88if ( $idcount2 != $idcounts ) {
89
90echo "<hr color="#900000" size="1"/>".
91
92"<div style="font-size:20pt;font-family:times;font-weight:bold"> The fields of two tables doesn't match" ;
93
94echo "<br/><br/>ODBC_table Fields = " . $idcounts;
95
96echo "<br/><br/>MySQL_table Fields = " . $idcount2;
97}
98?>
99
100
101</div></div></body>
102</html>
1未完接(二)