Home Database Mysql Tutorial SQL Server数据库导入MySQL数据库的体验_MySQL

SQL Server数据库导入MySQL数据库的体验_MySQL

Jun 01, 2016 pm 02:11 PM
mysql experience import data database

SQL 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的数据库中保存。
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1675
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

Explain the purpose of foreign keys in MySQL. Explain the purpose of foreign keys in MySQL. Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

Compare and contrast MySQL and MariaDB. Compare and contrast MySQL and MariaDB. Apr 26, 2025 am 12:08 AM

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

MySQL: The Database, phpMyAdmin: The Management Interface MySQL: The Database, phpMyAdmin: The Management Interface Apr 29, 2025 am 12:44 AM

MySQL and phpMyAdmin can be effectively managed through the following steps: 1. Create and delete database: Just click in phpMyAdmin to complete. 2. Manage tables: You can create tables, modify structures, and add indexes. 3. Data operation: Supports inserting, updating, deleting data and executing SQL queries. 4. Import and export data: Supports SQL, CSV, XML and other formats. 5. Optimization and monitoring: Use the OPTIMIZETABLE command to optimize tables and use query analyzers and monitoring tools to solve performance problems.

SQL vs. MySQL: Clarifying the Relationship Between the Two SQL vs. MySQL: Clarifying the Relationship Between the Two Apr 24, 2025 am 12:02 AM

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

Redis: Understanding Its Architecture and Purpose Redis: Understanding Its Architecture and Purpose Apr 26, 2025 am 12:11 AM

Redis is a memory data structure storage system, mainly used as a database, cache and message broker. Its core features include single-threaded model, I/O multiplexing, persistence mechanism, replication and clustering functions. Redis is commonly used in practical applications for caching, session storage, and message queues. It can significantly improve its performance by selecting the right data structure, using pipelines and transactions, and monitoring and tuning.

Steps to add and delete fields to MySQL tables Steps to add and delete fields to MySQL tables Apr 29, 2025 pm 04:15 PM

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

See all articles