ACCESS数据库向MySQL快速迁移小程序(二)_MySQL
Release: 2016-06-01 14:12:11
Original
830 people have browsed it
Access
ODBC --> MySQL Migrant
$dbconnection = @mysql_connect("yourmysqlserver", "mysqlaccount", "mysqlpassword")
or die ("can not connect to database server");
@mysql_select_db("yourdatabase")
or die("No Database,
") ;
$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't match ") ;
echo "n" ;
echo "
n " ; for ( $i = 1 ; $i echo "n |
" . odbc_field_name($recordsid,$i) ; $theno = 1 ; echo " |
n" ;
while (odbc_fetch_row($recordsid) ) {
$runsql = "insert into " . $table2 . "(" . $fd2 . ") values (" ;
for ( $i = 1 ; $i
$fdv = odbc_result($recordsid,$i) ;
if ( mysql_field_type($result,$i-1) == "string")
$runsql .= "'". $fdv . "'," ;
else
$runsql .= $fdv. "," ;
}
$runsql = substr($runsql,0,strlen($runsql)-1) ;
$runsql .= ")" ;
mysql_query ($runsql) ;
$theno++ ;
}
echo "Total Convert : " . $theno -- ;
odbc_close($conn);
?>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
有经验的网友可能看了两个小程序,可能觉得如在ACCESS数据库中各表的字段是英文的话,fdlist.php有点多余,其实在这里,我的本意是在迁移时,可以很方便地删除某些不用的字段,只要将fdlist.php的生成文件fdlist.txt中对应的字段Delete掉即可。还算是很方便的,反正,有了这两个小程序,我以前放在ACCESS中的几十个表格的数据,二十分钟全部迁移完毕,包括对其中四、五个表格字段的修改。
当然,虽然两个程序达到了我预定的功能,但要将其扩展为所有人可用其将ODBC数据库迁移至MySQL数据库,且使用更加方便一些,还有一些方面可以改进,如根据ODBC表格的结构自动建立MySQL表格,如不能建立,给出一步一步用户建立表格的向导功能;检测源表和目的表格数据类型的差异...等等,不过,我的数据迁移任务已完成,编这样的程序失去了一半的原动力,各位要是给我一点鼓励的话,我也许会做出一个更加完美的程序来,精神上的鼓励也行,当然物质上的就更佳了(喂!不是西红柿和香蕉皮)。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31