pandas连接mysql数据库

pandas连接mysql数据库

1.安装依赖 "UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other # DBAPI2 objects are not tested. Please consider using SQLAlchemy."

conda install sqlalchemy pymysql

2.导入类库

from sqlalchemy import create_engine

import pandas as pd

3.创建数据库连接字符串

user = 'root'

password = '123456'

host = '127.0.0.1'

port = '3306'

database = 'lrcore_cloud_xxx'

db_url = f'mysql+pymysql://{user}:{password}@{host}:{port}/{database}'

4.创建 SQLAlchemy 引擎

engine = create_engine(db_url)

5.使用 Pandas 读取数据

df = pd.read_sql_table('user', engine)

print(df)

'''

输出结果

id name

0 1 jack

1 2 boom

2 3 lucy

3 4 jack

4 5 boom

5 6 lucy

'''

相关推荐
旅僧11 分钟前
运行UMI镜像
python
ellenwan202613 分钟前
期货跨期价差程序化怎么做:天勤 SP 合约与腿比例核对
python·区块链
月疯16 分钟前
torch:expand和repeate的区别
开发语言·python·深度学习
顾林海35 分钟前
Agent入门阶段-编程基础-Python:Python 开发环境与运行方式
python·agent·ai编程
叫我:松哥37 分钟前
基于深度卷积神经网络的水果图片分类算法设计与实现,有ResNet50的迁移学习模型,准确率达95%
人工智能·python·神经网络·机器学习·分类·cnn·迁移学习
Orchestrator_me1 小时前
Python pip install报SSL错误
python·ssl·pip
开源量化GO1 小时前
期货 K 线算信号 tick 级止损:天勤双序列 wait_update 触发规则
linux·运维·服务器·python
聆春烟雨簌簌1 小时前
LangChain4j使用文档
开发语言·python
belong_my_offer1 小时前
在Pycharm中安装conda的保姆级教学
python
CTA终结者1 小时前
期货量化合约代码写错:天勤 symbol 格式与 silent 订阅坑
python·区块链