乐趣区

R和python分别连接mysql80及mongodb42数据库

1.python 连接 mysql8.0 数据库

import pandas as pd
from sqlalchemy import create_engine
engine = create_engine('mysql+mysqlconnector://root:123456@localhost:3306/ssm')
sql = 'select * from teacher;'
df = pd.read_sql_query(sql,engine)
print(df)

2.R 连接 mysql8.0 数据库 (自行配置 ODBC)

library(RODBC) 
channel <- odbcConnect("RMySQL",uid = "root",pwd = "123456")
crimedat <- sqlQuery(channel,"select * from teacher")
print(crimedat)

3.python 连接 mongodb4.2 数据库 (本人 mongo 没设密码)

from pymongo import MongoClient
connect = MongoClient('mongodb://localhost:27017/')
db = connect['analysis']
collection = db['student']
collection.find_one()

4.R 连接 mongodb4.2 数据库 (本人 mongo 没设密码)

library(mongolite)
con <- mongo(collection = "student",db = "analysis", url = "mongodb://localhost")
con$find()
退出移动版