找到一种不错的从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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.
