赞
踩
from sqlalchemy import create_engine
一个MySQL信息如下:
username = root
password = cbj123
host = 127.0.0.1
port = 3306
database = bigdata
则连接数据库时应该这样写:
db111 = 'mysql+pymysql://username:password@host:port/database?charset=utf8'
即:
db111 = 'mysql+pymysql://root:cbj123@127.0.0.1:3306/bigdata?charset=utf8'
上面两步做好后接着写如下代码即可:
engine = create_engine(DB_URI)
conn = engine.connect()
result = conn.execute("select * from 你数据库里的一个表")
print(result.fetchone())
如有输出信息则连接成功!!!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。