欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實現(xiàn)讀取SQLServer數(shù)據(jù)并插入到MongoDB數(shù)據(jù)庫的方法示例

 更新時間:2018年06月09日 12:56:36   作者:shaomine  
這篇文章主要介紹了Python實現(xiàn)讀取SQLServer數(shù)據(jù)并插入到MongoDB數(shù)據(jù)庫的方法,涉及Python同時進行SQLServer與MongoDB數(shù)據(jù)庫的連接、查詢、讀取、寫入等相關(guān)操作實現(xiàn)技巧,需要的朋友可以參考下

本文實例講述了Python實現(xiàn)讀取SQLServer數(shù)據(jù)并插入到MongoDB數(shù)據(jù)庫的方法。分享給大家供大家參考,具體如下:

# -*- coding: utf-8 -*-
import pyodbc
import os
import csv
import pymongo
from pymongo import ASCENDING, DESCENDING
from pymongo import MongoClient
import binascii
'''連接mongoDB數(shù)據(jù)庫'''
client = MongoClient('10.20.4.79', 27017)
#client = MongoClient('10.20.66.106', 27017)
db_name = 'SoftADoutput'
db = client[db_name]
'''連接SqlServer數(shù)據(jù)庫'''
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數(shù)據(jù)插入##########################
'''從SQLServer數(shù)據(jù)庫讀取Channel_CovCode數(shù)據(jù)寫入到mongodb數(shù)據(jù)庫中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數(shù)據(jù)插入#############################
'''從SQLServer數(shù)據(jù)庫讀取channel_ModeCode數(shù)據(jù)寫入到mongodb數(shù)據(jù)庫中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數(shù)據(jù)插入########################
'''從SQLServer數(shù)據(jù)庫讀取citynumb數(shù)據(jù)寫入到mongodb數(shù)據(jù)庫中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數(shù)據(jù)插入############################
'''從SQLServer數(shù)據(jù)庫讀取channel數(shù)據(jù)寫入到mongodb數(shù)據(jù)庫中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數(shù)據(jù)插入############################
'''從SQLServer數(shù)據(jù)庫讀取CPBZK數(shù)據(jù)寫入到mongodb數(shù)據(jù)庫中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數(shù)據(jù)插入##########################
'''從SQLServer數(shù)據(jù)庫讀取TVPGMCLASS數(shù)據(jù)寫入到mongodb數(shù)據(jù)庫中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數(shù)據(jù)插入###########################
'''從SQLServer數(shù)據(jù)庫讀取GGBZK_DESCRIPTION數(shù)據(jù)寫入到mongodb數(shù)據(jù)庫中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數(shù)據(jù)插入##########################
'''從SQLServer數(shù)據(jù)庫讀取Z201607_027數(shù)據(jù)寫入到mongodb數(shù)據(jù)庫中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)
    #先把時間戳轉(zhuǎn)為字符串,然后再轉(zhuǎn)為十進制數(shù)
    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)
#############################################################################################

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python常見數(shù)據(jù)庫操作技巧匯總》、《Python編碼操作技巧總結(jié)》、《Python圖片操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對大家Python程序設(shè)計有所幫助。

相關(guān)文章

  • python目錄操作之python遍歷文件夾后將結(jié)果存儲為xml

    python目錄操作之python遍歷文件夾后將結(jié)果存儲為xml

    需求是獲取服務(wù)器某個目錄下的某些類型的文件,考慮到服務(wù)器即有Linux、又有Windows,所以寫了一個Python小程序來完成這項工作,大家參考使用吧
    2014-01-01
  • python如何派生內(nèi)置不可變類型并修改實例化行為

    python如何派生內(nèi)置不可變類型并修改實例化行為

    這篇文章主要為大家詳細介紹了python如何派生內(nèi)置不可變類型并修改實例化行為,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • 詳解Python中的偏函數(shù)(Partial Functions)

    詳解Python中的偏函數(shù)(Partial Functions)

    Python中的偏函數(shù)是來自函數(shù)式編程的一個強大工具,它的主要目標(biāo)是減少函數(shù)調(diào)用的復(fù)雜性這個概念可能起初看起來有點困難理解,但一旦你明白了它的工作方式,它可能會成為你的編程工具箱中的重要組成部分,文中有相關(guān)的代碼介紹,需要的朋友可以參考下
    2023-06-06
  • django如何設(shè)置csrf_token

    django如何設(shè)置csrf_token

    這篇文章主要介紹了django如何設(shè)置csrf_token問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Python程序流程控制實驗

    Python程序流程控制實驗

    這篇文章主要介紹了Python程序流程控制實驗,程序流程控制的使用在Python中具有非常重要的地位。在本文中我將布置Python程序流程控制的實驗習(xí)題,希望您閱讀完本文后能夠有所收獲
    2022-01-01
  • Python錯誤處理操作示例

    Python錯誤處理操作示例

    這篇文章主要介紹了Python錯誤處理操作,結(jié)合實例形式分析了Python使用try...except...finaly語句進行錯誤處理的相關(guān)操作技巧與注意事項,需要的朋友可以參考下
    2018-07-07
  • Python調(diào)用百度AI實現(xiàn)顏值評分功能

    Python調(diào)用百度AI實現(xiàn)顏值評分功能

    這篇文章主要介紹了應(yīng)用百度AI的人臉識別功能對年齡、性別、顏值等進行識別,代碼具有一定的學(xué)習(xí)價值,感興趣的朋友跟隨小編一起看看吧
    2021-11-11
  • Python實現(xiàn)圖像的二進制與base64互轉(zhuǎn)

    Python實現(xiàn)圖像的二進制與base64互轉(zhuǎn)

    這篇文章主要為大家介紹了如何在Python中使用OpenCV從而實現(xiàn)圖像轉(zhuǎn)base64編碼、圖像轉(zhuǎn)二進制編碼、二進制轉(zhuǎn)圖像等功能,感興趣的可以跟上小編一起學(xué)習(xí)一下
    2022-03-03
  • 通過python爬蟲mechanize庫爬取本機ip地址的方法

    通過python爬蟲mechanize庫爬取本機ip地址的方法

    python中的mechanize算是一個比較古老的庫了,在python2的時代中,使用的多一些,在python3以后就很少使用了,現(xiàn)在已經(jīng)是2202年了,可能很多人都沒聽說過mechanize,這不要緊,我們先來簡單的講解一下,如何使用mechanize,感興趣的朋友一起看看吧
    2022-08-08
  • 利用Python實現(xiàn)自動生成數(shù)據(jù)日報

    利用Python實現(xiàn)自動生成數(shù)據(jù)日報

    日報,是大部分打工人繞不過的難題。對于管理者來說,日報是事前管理的最好抓手,可以了解團隊的氛圍和狀態(tài)。本文將利用Python實現(xiàn)自動生成數(shù)據(jù)日報,感興趣的可以動手嘗試一下
    2022-07-07

最新評論