找到一种不错的从SQLServer转成Mysql数据库的方法
找到一种不错的从SQLServer转成Mysql数据库的方法
年初的时候一直在做一个网站MSSQL2000 -> MySQL5的迁移工作,因为采用了不同的程序系统,所以主要问题在数据的迁移。由于2个系统数据库结构差异非常大,不方便采取SQL SERVER里导入MYSQL的ODBC数据源的功能(也不推荐这么做,字段类型等不同点会搞死人的~),因此就在WINDOWS下自己写PHP脚本从SQLSERVER里读数据,然后按照新系统的需要处理之后插入MYSQL里面,灵活也比较方便。实际过程主要有下面几个问题:1、数据库的连接,主要是连接SQL SERVER。主要有3种方法:
1.1 利用PHP中的mssql_系列函数,这个与使用mysql_系列函数类似,不过要打开php.ini中相关扩展(extension=php_mssql.dll)。
1.2 利用ODBC连接,由于抽象了具体数据库,所以没有办法利用数据表字段名=>数组键名的特性,在针对具体应用时不是很方便,代码形式:
$conn = odbc_connect("datasource","username","password");
$sql = "select * from news";
$cur= odbc_exec($conn, $sql);
while(odbc_fetch_row($cur)){
$field1 = odbc_result($cur,1);
$field2 = odbc_result($cur,2);
//do something
}
1.3 使用PDO - PHP5中加入数据对象抽象层,作为官方推出的数据访问接口,优点有很多,比如支持参数绑定以防止SQL注入;对于不同数据库加载不同驱动即可,程序代码是一致的,便于移植等等,相信应该是大势所趋。不过由于用了PHP5全新的面向对象特性,需要PHP5的支持,5.1可以直接使用,5需要装PECL,另外还要修改PHP.ini,增加:extension=php_pdo_mysql.dll和extension=php_pdo_mssql.dll,实际代码如下:
try {
$DBH=new PDO("mssql:dbname=XXX;host=localhost",
"root", "password");//Connect to DB
} catch (PDOException $e) {
print "Error!: " . $e->getMessage();//Error Message
die();
}
$stmt = $DBH->prepare("SELECT * FROM news");//Stmt Handle $stmt
if ($stmt->execute()) {
while ($row = $stmt->fetch()) {
//do something
}
}
$stmt2 = $mssql->prepare("INSERT INTO news
(title,author) VALUES (:title, :author)");
$stmt2->bindParam(':title', $title);
$stmt2->bindParam(':author', $author);
$stmt2->execute();
$DBH = null;// Close Connection这里要提醒下的是MSSQL里面是没有MYSQL中LIMIT这个语法的。
2、TEXT字段被截断的问题。
上面尝试了3种连接数据库方法,是因为当初连上MSSQL后SELECT出来的数据总是只有4K长度,以为是连接方式限制导致的,所以换了几种都是这样,最后查了资料才知道,是php.ini里面这2句配置的问题:
; Valid range 0 - 2147483647. Default = 4096.
mssql.textlimit = 4096
; Valid range 0 - 2147483647. Default = 4096.
mssql.textsize = 4096
把4096改成-1(代表无限制)即可,也可以使用mssql_query("SET TEXTSIZE 65536");来实现。
3.两种数据库字段类型不同的问题有2个地方需要讲一下,一个是字段支持最大长度要注意,以免插入数据库时候被截断,另外一个就是日期格式的问题了,我比较喜欢用UNIX时间戳。在连MSSQL时候可以用 "select unix_timestamp(created) from news" 来实现MSSQL里面DATETIME到MYSQL里时间戳的转换。不过迁移时候一次要提取表中所有字段,像上面这种方法就没有简单的"select * from news"简洁,需要罗列所有字段。实际可以直接SELECT出来,得到的是一个字符串,比如在MSSQL里面是2006-01-01 12:01,取出来的字符串是"2006 一月 01 12:01"(有些奇怪,不知道为什么会产生中文)。用下面这个函数可以转换成时间戳:
function ConvertTime($timestring){
if($timestring == null){
return 0;
}
$time = explode(" ",$timestring);
$year = $time[0];
switch ($time[1]){
case "一月":$month = "1";break;
case "二月":$month = "2";break;
case "三月":$month = "3";break;
case "四月":$month = "4";break;
case "五月":$month = "5";break;
case "六月":$month = "6";break;
case "七月":$month = "7";break;
case "八月":$month = "8";break;
case "九月":$month = "9";break;
case "十月":$month = "10";break;
case "十一月":$month = "11";break;
case "十二月":$month = "12";break;
default:break;
}
$day = $time[2];
$h = 0;
$m = 0;
$s = 0;
if(!empty($time[3])){
$time2 = explode(":",$time[3]);
$h = $time2[0];
$i = $time2[1];
}
//return date("Y-m-d H:i:s",mktime($h,$i,$s,$month,$day,$year));
return mktime($h,$i,$s,$month,$day,$year);
}最后转换脚本写完了可以在CMD窗口里面用php.exe abc.php来执行,这种方式是没有超时时间的,适合迁移大批量数据。
基本上就是这些内容,希望对大家有帮助。
Update(06/05/05):关于时间戳的转换,在php.ini中加上
mssql.datetimeconvert = Off
后就能得到类似2006-01-01 12:01不带有中文的格式了。

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values and pointers to data rows, and is suitable for non-primary key column queries.

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.
