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的数据库中保存。
时间匆忙,先总结到这里,希望这些文字能对有需要的人有些帮助,少走些弯路,感谢您的阅读。:)

mysql的msi与zip版本的区别:1、zip包含的安装程序是一种主动安装,而msi包含的是被installer所用的安装文件以提交请求的方式安装;2、zip是一种数据压缩和文档存储的文件格式,msi是微软格式的安装包。

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。

在mysql中,可以利用REGEXP运算符判断数据是否是数字类型,语法为“String REGEXP '[^0-9.]'”;该运算符是正则表达式的缩写,若数据字符中含有数字时,返回的结果是true,反之返回的结果是false。

在mysql中,是否需要commit取决于存储引擎:1、若是不支持事务的存储引擎,如myisam,则不需要使用commit;2、若是支持事务的存储引擎,如innodb,则需要知道事务是否自动提交,因此需要使用commit。

“mysql-connector”是mysql官方提供的驱动器,可以用于连接使用mysql;可利用“pip install mysql-connector”命令进行安装,利用“import mysql.connector”测试是否安装成功。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
