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

'''

相关推荐
失败尽常态52326 分钟前
用Python实现Excel数据同步到飞书文档
python·excel·飞书
2501_9044477428 分钟前
OPPO发布新型折叠屏手机 起售价8999
python·智能手机·django·virtualenv·pygame
青龙小码农28 分钟前
yum报错:bash: /usr/bin/yum: /usr/bin/python: 坏的解释器:没有那个文件或目录
开发语言·python·bash·liunx
大数据追光猿34 分钟前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
Leuanghing1 小时前
【Leetcode】11. 盛最多水的容器
python·算法·leetcode
xinxiyinhe2 小时前
如何设置Cursor中.cursorrules文件
人工智能·python
诸神缄默不语3 小时前
如何用Python 3自动打开exe程序
python·os·subprocess·python 3
橘子师兄3 小时前
分页功能组件开发
数据库·python·django
Logintern093 小时前
使用VS Code进行Python编程的一些快捷方式
开发语言·python
Multiple-ji4 小时前
想学python进来看看把
开发语言·python