mysql数据导入mongo---python实现

 

import pymysql
import pymongo

# 创建mysql的数据库连接
con = pymysql.connect(host='', port='', user='', password='', db='')
# 获取游标
cur = con.cursor(cursor=pymysql.cursors.DictCursor)
# 查询student表
result =[]
try:
  cur.execute('select * from disease')
  client = pymongo.MongoClient(host='', port='')
  db = client['disease_knowledge']
  for row in cur.fetchall():
      if row['jbmc'] not in result:
          print(row)
          db.knowledge.insert_one(row)
          result.append(row['jbmc'])
except Exception as e:
  print(e)
finally:
  con.close()
  client.close()

  

posted @ 2020-08-20 13:58  boblee12  阅读(137)  评论(0)    收藏  举报