mysqlデータmongodbへのインポート

3207 ワード

#  mysql         mongodb 
import pymysql
import pymongo

#   mysql      
con = pymysql.connect(host='localhost', port=3306, user='root', password='123456', db='pp')
#     
cur = con.cursor(cursor=pymysql.cursors.DictCursor)
#   student 
try:
  cur.execute('select * from student')
  #   mongodb     
  client = pymongo.MongoClient(host='localhost', port=27017)
  #      
  db = client['pp']#  db=client.pp,        use pp;
  for row in cur.fetchall():
    row['birthday'] = str(row['birthday']) #  mongodb  datetime  ,              mongodb,       
    db.student.insert_one(row)
except Exception as e:
  print(e)
finally:
  con.close()
  client.close()
#The achievement is attributed to teacher Peng!

転載先:https://www.cnblogs.com/zpdbkshangshanluoshuo/p/10065311.html