談談資料從sql server資料庫匯入mysql資料庫的體驗(原創)

來源:互聯網
上載者:User
mysql|server|資料|資料庫|原創 因工作需要,要將存放在sql server資料庫中的資料全部匯入到mysql資料庫中,在網上搜集相關資料,找到兩種方法,現在分別談談對他們的看法。

第一種是安裝mysql ODBC,利用sql server的匯出功能,選擇mysql資料來源,進行資料的直接匯出,這種方法很簡便,但是針對實際應用有很多弊端,最主要體現就是資料類型問題,首先,sql server資料庫中
的ntext,image等資料類型的資料無法直接寫入到mysql資料庫中,據說只要稍加改動就可以,可惜偶這隻菜鳥還沒想到如何改動,其次,因為偶在mysql中的資料庫設計中將時間都設成int型(儲存的是時間戳記),所以在資料導過來後,就會出現衝突,再次,這種方法產生的mysql資料表的欄位類型都不很合適,所以此種方法我覺得不能提倡。

第二種是利用php或asp指令碼來實現資料的匯入功能,這種方法需要編寫程式,但靈活性大,操作也不是那麼困難,一切都盡在你的掌握之中,現簡單介紹一下該方法
前提條件是你的mysql環境已經搭建好了,先建好目標資料庫,再將所有的表結構用sql語句產生,現在萬事具備,只缺資料了。

可以通過下面的php指令碼來實現sql server中mydb資料庫的user表中資料向mysql中mydb資料庫匯入
<?
$cnx = odbc_connect('web', 'admin', '123456');//'web'是sqlserver中mydb的資料來源名,'admin'是訪問mydb的使用者名稱,'123456'是訪問mydb的密碼
$cur= odbc_exec( $cnx, 'select *  from user' );//開啟sql server中mydb資料庫的user表
$num_row=0;
$conn=mysql_pconnect("localhost","root","123456");// 串連mysql
@mysql_select_db('mydb',$conn) or

die("無法串連到資料庫,請與管理員聯絡!");//開啟mysql的mydb資料庫
while( odbc_fetch_row( $cur ))            //從sql server的mydb庫中的user表逐條取出資料,如果對資料進行選擇,可在前面的select語句中加上條件判斷
{
  $num_row++;
  $field1 = odbc_result( $cur, 1 );   // 這裡的參數i(1,2,3..)指的是記錄集中的第i個域,你可以有所選擇地進行選取,fieldi得到對應域的值,然後你可以對fieldi進行操作
  $field2 = odbc_result( $cur, 2 );   
  $field3 = odbc_result( $cur, 3 );   
  $field4 = odbc_result( $cur, 4 );   
  $field5 = odbc_result( $cur, 5 );   
  $field6 = odbc_result( $cur, 6 );   
  $field5 = timetoint($field5);    //這裡是對sql server中的datetime類型的欄位進行相應轉換處理,轉換成我所需要的int型   
  $querystring = "insert into user
                  (id,name,username,password,recdate)
                values('$field1','$field2','$field3','$field4','$field5')" ;

  mysql_query($querystring,$conn);
}

function timetoint($str){
  $arr1=split(" ",$str);
  $datestr=$arr1[0];
  $timestr=$arr1[1];
  $arr_date=split("-",$datestr);
  $arr_time=split(":",$timestr);
  $year=$arr_date[0];
  $month=$arr_date[1];
  $day=$arr_date[2];
  $hour=$arr_time[0];
  $minute=$arr_time[1];
  $second=$arr_time[2];
  $time_int=mktime($hour,$minute,$second,$month,$day,$year);
  return $time_int;
}
?>

將該段指令碼存成sql.php,在伺服器上執行,就可以將伺服器上sql server中mydb資料庫的user表中的資料匯入到mysql中mydb資料庫的user表中去。其他表的操作與此雷同,就不贅述了。

下面再介紹一下asp指令碼實現sql server中mydb資料庫的資料向mysql中mydb資料庫匯入
<%
set conn=server.createobject("adodb.connection")
conn.open 'web', 'admin', '123456' // 'web'是sqlserver中mydb的資料來源名,'admin'是訪問mydb的使用者名稱,'123456'是訪問mydb的密碼
set rs=server.createobject("adodb.recordset")
sql="select ID,name,username,password,datediff(s,'1970-01-01 00:00:00',recdate)-8*3600,reid,filename,fileContentType,filevalue from senddate" //這條sql語句實現了將datetime類型的recdate欄位轉化成unix時間戳記的int型

rs.open sql,conn,1,3
set conn1=server.createobject("adodb.connection")
conn1.open "myoa","root","q1-d6=7?"
i=1
do while not rs.eof
  field1 = rs(0)   
  field2 = rs(1)   
  field3 = rs(2)   
  field4 = rs(3)   
  field5 = rs(4)   
  sql1 = "insert into user(ID,name,username,password,recdate)        

values("&field1&",'"&field2&"','"&field3&"','"&field4&"',"&field5&")"

conn1.execute sql1
rs.movenext
i=i+1
loop
rs.close
set rs=nothing
conn.close
set conn=nothing
conn1.close
set conn1=nothing

%>

以上兩個是分別採用php指令碼和asp指令碼對user表的資料進行由sql server到mysql的匯入其間我採用2種迴避的方法來避免ntext,image類型資料的傳遞,一種是將ntext欄位改為nvarchar(4000),因為實際情況,未經處理資料中該欄位的資料長度都未超過4000個字,所以並沒有出現資料截斷,另一個手段是將image類型資料取出來寫到檔案中,以檔案形式儲存,將檔案路徑存到資料庫中,方法見下:

function makeattach(fileContentType,filevalue,i)
    select case fileContentType
        case "application/msword"
            ext="doc"

        case "application/vnd.ms-excel"
            ext="exl"
            
        case "application/vnd.ms-powerpoint"
            ext="pps"
            
        case "application/x-rar-compressed"
            ext="rar"
            
        case "application/x-zip-compressed"
            ext="zip"
            
        case "image/gif"
            ext="gif"
            
        case "image/pjpeg"
            ext="jpg"
            
        case "text/plain"
            ext="txt"
            
        case else
            ext="x"
            
    end select
    if ext<>"x" then
        set fso=server.createobject("FileSystemObject")
        fName="attech"&i&"."&ext
        Dir="d:\attach\"
        If fso.FileExists(Dir & fName) Then fso.deletefile Dir & fName
        If fName<>"" AND NOT fso.FileExists(Dir & fName) Then
            Set strm1=Server.CreateObject("ADODB.Stream")
            strm1.Open
            strm1.Type=1 'Binary
            strm1.Write filevalue
            strm1.SaveToFile Dir & fName,2
            Set strm1=Nothing
        end if
        makeattach=fName
    end if
end function

這個函數有3個輸入參數,第一個是檔案的contentType,第二個是檔案的位元值,第三個是個可以區別檔案名稱的變數,先根據contentType確定所存檔案的尾碼名,然後就是將位元值儲存成指定檔案名稱的檔案,並將檔案名稱作為輸出參數返回,將返回的參數作為資料寫到mysql的資料庫中儲存。
時間匆忙,先總結到這裡,希望這些文字能對有需要的人有些協助,少走些彎路,感謝您的閱讀。:)


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.