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

'''

相关推荐
m0_613856291 小时前
mysql如何利用事务隔离级别解决特定业务冲突_mysql隔离方案选型
jvm·数据库·python
我的xiaodoujiao2 小时前
API 接口自动化测试详细图文教程学习系列16--项目实战演练3
python·学习·测试工具·pytest
ID_180079054732 小时前
Python 实现亚马逊商品详情 API 数据准确性校验(极简可用 + JSON 参考)
java·python·json
时空系2 小时前
第10篇:继承扩展——面向对象编程进阶 python中文编程
开发语言·python·ai编程
CHANG_THE_WORLD4 小时前
python 批量终止进程exe
开发语言·python
liann1194 小时前
3.2_红队攻击框架--MITRE ATT&CK‌
python·网络协议·安全·网络安全·系统安全·信息与通信
云天AI实战派4 小时前
AI 智能体问题排查指南:ChatGPT、API 调用到 Agent 上线失灵的全流程修复手册
大数据·人工智能·python·chatgpt·aigc
我的xiaodoujiao4 小时前
API 接口自动化测试详细图文教程学习系列15--项目实战演练2
python·学习·测试工具·pytest
多思考少编码5 小时前
PAT甲级真题1001 - 1005题详细题解(C++)(个人题解)
c++·python·最短路·pat·算法竞赛
ZhengEnCi6 小时前
M5-markconv自定义CSS样式指南 📝
前端·css·python