SQL Server数据库连接
(1)SQL server 数据库连接需要pyodbc模块,而pyodbc的需要配置相应的开发环境
具体的连接: [link](步骤 1: 配置 pyodbc Python 开发环境 - SQL Server | Microsoft Docs https://docs.microsoft.com/zh-cn/sql/connect/python/pyodbc/step-1-configure-development-environment-for-pyodbc-python-development?view=sql-server-2017).
(2) 开发环境配置完成之后,使用pip 安装pyodbc模块,但是会报错
此时: 没有安装相应的开发环境,安装相应版本的Sql Server Management Studio即可
Oracle数据库连接
借助Python包cx_Oracle
代码实现从Sql Server数据库搬移表数据到Oracle:
import pyodbc
import cx_Oracle
table = table_info
## 建立sql Server数据库连接
ss_cn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=' + '172.16.2.15' + ';DATABASE=' + 'xn_testdb' + ';UID=' + 'sa' + ';PWD=' + '****')
# 创建游标,依次访问数据
cursor = ss_cn.cursor()
cursor.execute('select * from %s'%table) # 使用变量作为表名
# 创建Oracle连接
cx_cn = cx_Oracle.connect('scott/***@172.16.0.0/oracl')
conn = cx_cn.cursor()
conn.execute("truncate table %s"%table)
# 从SQl Server读取数据
row = cursor.fetchall()
for i in range(len(row)):
conn.execute("insert into %s values(%s,'%s',1)" %(table,row[i][0],row[i][1]))