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

'''

相关推荐
程序猿阿伟7 小时前
《Python生态事件溯源与CQRS轻量化落地指南》
大数据·python·微服务
王夏奇7 小时前
python在汽车电子行业中应用2—具体包的介绍和使用
网络·python·汽车
Dxy12393102167 小时前
Python的zip用法详解
开发语言·python
我的golang之路果然有问题7 小时前
python中 unicorn 热重启问题和 debug 的 json
java·服务器·前端·python·json
lagrahhn7 小时前
scoop的使用
大数据·python·搜索引擎
智算菩萨7 小时前
【Python自然语言处理】实战项目:词向量表示完整实现指南
开发语言·python·自然语言处理
Elaine3367 小时前
【验证码识别算法性能对比实验系统——KNN、SVM、CNN 与多模态大模型的性能博弈与机理分析】
python·opencv·支持向量机·cnn·多模态·数字图像处理
SCBAiotAigc7 小时前
langchain1.x学习笔记(三):langchain之init_chat_model的新用法
人工智能·python·langchain·langgraph·deepagents
Blossom.1188 小时前
联邦迁移学习实战:在数据孤岛中构建个性化推荐模型
开发语言·人工智能·python·深度学习·神经网络·机器学习·迁移学习
Blossom.1188 小时前
大模型自动化压缩:基于权重共享的超网神经架构搜索实战
运维·人工智能·python·算法·chatgpt·架构·自动化