python 读取SQLServer数据插入到MongoDB数据库中
# -*- coding: utf-8 -*-
import pyodbc
import os
import csv
import pymongo
from pymongo import ASCENDING, DESCENDING
from pymongo import MongoClient
import binascii
'''连接mongoDB数据库'''
client = MongoClient('10.20.4.79', 27017)
#client = MongoClient('10.20.66.106', 27017)
db_name = 'SoftADoutput'
db = client[db_name]
'''连接SqlServer数据库'''
connStr = 'DRIVER={SQL Server Native Client 11.0};SERVER=DESKTOP-44P34L6;DATABASE=Softput;UID=sa;PWD=sa'
conn = pyodbc.connect(connStr)
cursor = conn.cursor()
#########################################Channel_CovCode数据插入##########################
'''从SQLServer数据库读取Channel_CovCode数据写入到mongodb数据库中Channel_CovCode集合中'''
def InsertChannel_CovCode(cursor):
cursor.execute("select dm, ms from channel_CovCode")
rows = cursor.fetchall()
i = 1
for row in rows:#gb18030
db.channel_CovCode.insert({'_id':i,'dm':row.dm,'ms':row.ms.decode('gbk').encode('utf-8')})
i = i + 1
InsertChannel_CovCode(cursor)
#############################################################################################
#########################################channel_ModeCode数据插入#############################
'''从SQLServer数据库读取channel_ModeCode数据写入到mongodb数据库中channel_ModeCode集合中'''
def InsertChannel_ModeCode(cursor):
cursor.execute("select dm, ms from channel_ModeCode")
rows = cursor.fetchall()
i = 1
for row in rows:#gb18030
db.channel_ModeCode.insert({'_id':i,'dm':row.dm,'ms':row.ms.decode('gbk').encode('utf-8')})
i = i + 1
InsertChannel_ModeCode(cursor)
#############################################################################################
#########################################citynumb数据插入########################
'''从SQLServer数据库读取citynumb数据写入到mongodb数据库中citynumb集合中'''
def InsertCitynumb(cursor):
cursor.execute("select t.XZQMC,t.SMC,t.CSMC,t.SSQYDM,t.CITY_E,t.AREA_E,t.PROV_E from citynumb t")
rows = cursor.fetchall()
i = 1
for row in rows:
xzqmc = row.XZQMC
if xzqmc != None:
xzqmc = xzqmc.decode('gbk').encode('utf-8')
smc = row.SMC
if smc != None:
smc = smc.decode('gbk').encode('utf-8')
csmc = row.CSMC
if csmc != None:
csmc = csmc.decode('gbk').encode('utf-8')
db.citynumb.insert({'_id':i,'XZQMC':xzqmc,'SMC':smc,'CSMC':csmc,'SSQYDM':row.SSQYDM,'CITY_E':row.CITY_E,'AREA_E':row.AREA_E,'PROV_E':row.PROV_E})
i = i + 1
InsertCitynumb(cursor)
##################################################################################################################
#########################################channel数据插入############################
'''从SQLServer数据库读取channel数据写入到mongodb数据库中channel集合中'''
def InsertChannel(cursor):
cursor.execute("select pdcmc,pdemc,pdemcj,pdbm1,ssqydm,cov,sdate,mode,startTime,endTime,memo,pdtype,sflag,edate,corporation from channel")
rows = cursor.fetchall()
i = 1
for r in rows:
pdcmc = r.pdcmc
if pdcmc != None:
pdcmc = pdcmc.decode('gbk').encode('utf-8')
memo = r.memo
if memo != None:
memo = memo.decode('gbk').encode('utf-8')
corporation = r.corporation
if corporation != None:
corporation = corporation.decode('gbk').encode('utf-8')
db.channel.insert({'_id':i,'pdcmc':pdcmc,'pdemc':r.pdemc,'pdemcj':r.pdemcj,'pdbm1':r.pdbm1,'ssqydm':r.ssqydm,'cov':r.cov,'sdate':r.sdate,'mode':r.mode,'startTime':r.startTime,'endTime':r.endTime,'memo':memo,'pdtype':r.pdtype,'sflag':r.sflag,'edate':r.edate,'corporation':corporation})
i = i + 1
InsertChannel(cursor)
#############################################################################################
#########################################CPBZK数据插入############################
'''从SQLServer数据库读取CPBZK数据写入到mongodb数据库中CPBZK集合中'''
def InsertCPBZK(cursor):
cursor.execute("select ZTC,EZTC,ZTC_CODE,LBDM,B_CODE,QY_CODE,IChange,cla from CPBZK")
rows = cursor.fetchall()
i = 1
for r in rows:#gb18030
ztc = r.ZTC
if ztc != None:
ztc = ztc.decode('gbk').encode('utf-8')
db.CPBZK.insert({'_id':i,'ZTC':ztc,'EZTC':r.EZTC,'ZTC_CODE':r.ZTC_CODE,'LBDM':r.LBDM,'B_CODE':r.B_CODE,'QY_CODE':r.QY_CODE,'IChange':r.IChange,'cla':r.cla})
i = i + 1
InsertCPBZK(cursor)
#############################################################################################
#########################################TVPGMCLASS数据插入##########################
'''从SQLServer数据库读取TVPGMCLASS数据写入到mongodb数据库中TVPGMCLASS集合中'''
def InsertTVPGMCLASS(cursor):
cursor.execute("select ClassChDesc,ClassEnDesc,ClassCode,ParentCode,SortNo from TVPGMCLASS")
rows = cursor.fetchall()
i = 1
for r in rows:#gb18030
classChDesc = r.ClassChDesc
if classChDesc != None:
classChDesc = classChDesc.decode('gbk').encode('utf-8')
db.TVPGMCLASS.insert({'_id':i,'ClassChDesc':classChDesc,'ClassEnDesc':r.ClassEnDesc,'ClassCode':r.ClassCode,
'ParentCode':r.ParentCode,'SortNo':r.SortNo})
i = i + 1
InsertTVPGMCLASS(cursor)
#############################################################################################
#########################################GGBZK_DESCRIPTION数据插入###########################
'''从SQLServer数据库读取GGBZK_DESCRIPTION数据写入到mongodb数据库中GGBZK_DESCRIPTION集合中'''
def InsertGGBZK_DESCRIPTION(cursor):
cursor.execute("select V_code,des_named,des_main,des_background,des_scene,des_words,ModifyFlag,UpdateDate from GGBZK_DESCRIPTION")
rows = cursor.fetchall()
i = 1
for r in rows:#gb18030
name = r.des_named
if name != None:
name = name.decode('gbk').encode('utf-8')
desmain = r.des_main
if desmain != None:
desmain = desmain.decode('gbk').encode('utf-8')
background = r.des_background
if background != None:
background = background.decode('gbk').encode('utf-8')
scene = r.des_scene
if scene != None:
scene = scene.decode('gbk').encode('utf-8')
words = r.des_words
if words != None:
words = words.decode('gbk').encode('utf-8')
db.GGBZK_DESCRIPTION.insert({'_id':i,'V_code':r.V_code,'des_named':name,'des_main':desmain,'des_background':background,
'des_scene':scene,'des_words':words,'ModifyFlag':r.ModifyFlag,'UpdateDate':r.UpdateDate})
i = i + 1
InsertGGBZK_DESCRIPTION(cursor)
#########################################Z201607_027数据插入##########################
'''从SQLServer数据库读取Z201607_027数据写入到mongodb数据库中Z201607_027集合中'''
def InsertZ201607_027(cursor):
strSql = "select PD,RQ,SHIJIAN,ENDSHIJIAN,LBDM,ZTC_CODE,V_CODE,B_CODE,QY_CODE,QUANLITY,SPECIAL,LANGUAGE,LENGTH,SLENGTH,QJM1,QJM2,QGG,HJM1,HJM2,HGG,DUAN,OSHIJIAN,JG,SORTNO,LURU,ZFILE,COST,ROWTS,COST1,COST2,COST3 from Z201607_027"
cursor.execute(strSql)
rows = cursor.fetchall()
i = 1
for r in rows:#gb18030
cost = float(r.COST) #COST money类型
cost1 = float(r.COST1)
cost2 = float(r.COST2)
cost3 = float(r.COST3)
#先把时间戳转为字符串,然后再转为十进制数
rowts = int(str(binascii.b2a_hex(r.ROWTS)),16)
luru = r.LURU
if luru != None:
luru = luru.decode('gbk').encode('utf-8')
vCODE = r.V_CODE
if vCODE != None:
vCODE = vCODE.decode('gbk').encode('utf-8')
db.Z201607_027.insert({'_id':i,'PD':r.PD,'RQ':r.RQ,'SHIJIAN':r.SHIJIAN,'ENDSHIJIAN':r.ENDSHIJIAN,'LBDM':r.LBDM,
'ZTC_CODE':r.ZTC_CODE,'V_CODE':vCODE,'B_CODE':r.B_CODE,'QY_CODE':r.QY_CODE,'QUANLITY':r.QUANLITY,
'SPECIAL':r.SPECIAL,'LANGUAGE':r.LANGUAGE,'LENGTH':r.LENGTH,'SLENGTH':r.SLENGTH,'QJM1':r.QJM1,'QJM2':r.QJM2,'QGG':r.QGG,'HJM1':r.HJM1,'HJM2':r.HJM2,'HGG':r.HGG,'DUAN':r.DUAN,'OSHIJIAN':r.OSHIJIAN,'JG':r.JG,'SORTNO':r.SORTNO,'LURU':luru,'ZFILE':r.ZFILE,
'COST':cost,'ROWTS':rowts,'ExpandProperty':'','COST1':cost1,'COST2':cost2,'COST3':cost3})
i = i + 1
InsertZ201607_027(cursor)
#############################################################################################
python 读取SQLServer数据插入到MongoDB数据库中的更多相关文章
- python 读取excel数据插入到另外一个excel
#-*-coding:utf-8-*- import xlrd import xlwt def excel_copy(dir_from, dir_to, sheet_name): '''从一个exce ...
- Python爬取豆瓣音乐存储MongoDB数据库(Python爬虫实战1)
1. 爬虫设计的技术 1)数据获取,通过http获取网站的数据,如urllib,urllib2,requests等模块: 2)数据提取,将web站点所获取的数据进行处理,获取所需要的数据,常使用的技 ...
- 在MongoDB数据库中查询数据(上)
在MongoDB数据库中查询数据(上) 在MongoDB数据库中,可以使用Collection对象的find方法从一个集合中查询多个数据文档,find方法使用方法如下所示: collection.fi ...
- mongodb数据库中插入数据
mongodb数据库中插入数据 一:connection 访问集合: 在mongodb数据库中,数据是存储在许多数据集合中,可以使用数据库对象的collection方法访问一个集合.该方法使用如下: ...
- Python Json分别存入Mysql、MongoDB数据库,使用Xlwings库转成Excel表格
将电影数据 data.json 数据通过xlwings库转换成excel表格,存入mysql,mongodb数据库中.python基础语法.xlwings库.mysql库.pymongo库.mongo ...
- 使用mongify将sqlserver数据导入到mongodb
最近需要将sqlserver数据导入到mongodb中,在github上搜了一圈,发现两个项目有点适合 mongify sql2mongodb 先试了下sql2mongodb(有个好名字果然有好处啊) ...
- 记录python爬取猫眼票房排行榜(带stonefont字体网页),保存到text文件,csv文件和MongoDB数据库中
猫眼票房排行榜页面显示如下: 注意右边的票房数据显示,爬下来的数据是这样显示的: 网页源代码中是这样显示的: 这是因为网页中使用了某种字体的缘故,分析源代码可知: 亲测可行: 代码中获取的是国内票房榜 ...
- MongoDB数据库中更新与删除数据
MongoDB数据库中更新与删除数据 在MongoDB数据库中,可以使用Collection对象的update方法更新集合中的数据文档.使用方法如下所示: collection.update(sele ...
- 将DataTable 数据插入 SQL SERVER 数据库
原文:将DataTable 数据插入 SQL SERVER 数据库 以下提供3中方式将DataTable中的数据插入到SQL SERVER 数据库: 一:使用sqlcommand.executenon ...
随机推荐
- 使用angular的ng-repeat遇到的一个问题
问题描述:ng-repeat绑定的数据集动态更新之后其包裹的子元素所绑定的事件全部丢失:问题详述:问题代码如下: <style> img{width:100px;height:100px; ...
- mysql数据库常用语句
关于mysql数据库常用命令的整理: 一:对于数据库的操作 show databases;显示当前用户下所有的数据库名称 use database_name;进入当前数据库 create databa ...
- background不能填充margin的问题
CSS2 中有5个主要的背景(background)属性,它们是: background-color: 指定填充背景的颜色.background-image: 引用图片作为背景.background- ...
- otf字体转ttf字体
可以使用Font creator进行转换字体. 绿色版下载链接 Font Creator(字体编辑软件下载)V9.0官方版 或者我的百度云:http://pan.baidu.com/s/1c1jjfm ...
- SQL语句打印四个方向的9 9 乘法表
declare @i int ,@j int ,@s nvarchar(max) set @i = 1 while @i <=9 begin set @s = ' ' set @j = 1 wh ...
- 【Objective-C】2.自定义构造方法和description方法
1.Student.h 1 #import <Foundation/Foundation.h> 2 3 @interface Student : NSObject { 4 int _age ...
- 我的MFC学习之路(一)
因为项目需求,我开始应用MFC写程序.具体接触MFC的时间大概也有两个月了.现在的水平算是刚刚踏入了MFC大门的半只脚.目前能基本使用MFC Class Wizard,可以根据实例仿照完成需求,小范围 ...
- SSH无密码登陆问题解决
转载 http://my.oschina.net/hunzi/blog/10687 安装好Cygwin后,SSH需要设置为无密码登陆, 首先查看是ssh还是ssh2:ls -l `which ssh` ...
- 第一篇、CSS3_transtion的使用
<html> <head> <title>这是一个CSS3的特性</title> <style> #box{ width: 150px; h ...
- 12天学好C语言——记录我的C语言学习之路(Day 9)
12天学好C语言--记录我的C语言学习之路 Day 9: 函数部分告一段落,但是我们并不是把函数完全放下,因为函数无处不在,我们今后的程序仍然会大量运用到函数 //转入指针部分的学习,了解指针是什么 ...