python 读取excel数据存储到mysql

一、安装依赖

复制代码
pip install mysql-connector-python

二、mysql添加表students

sql 复制代码
CREATE TABLE `students` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `Name` varchar(50) DEFAULT NULL,
  `Sex` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8mb4;

三、代码.py

python 复制代码
import pandas as pd
from student import Student
import mysql.connector

students = []
df = pd.read_excel('students.xlsx', sheet_name='Sheet1')

for index, row in df.iterrows():
    s = Student(row["姓名"], row["性别"])
    students.append(s)

for i in range(len(students)):
    print(f'姓名:{students[i].name},性别:{students[i].sex}')
print('######################')
for i in range(len(students)):
    students[i].show()

config = {
  'user': 'root',
  'password': '123456',
  'host': '192.168.31.132',
  'database': 'demo'
}
db = mysql.connector.connect(**config)
cursor = db.cursor()
insertSql = 'INSERT INTO students(name,sex) VALUES (%s,%s)'
for i in range(len(students)):
    cursor.execute(insertSql,(students[i].name,students[i].sex))
db.commit()
print(f'插入{cursor.rowcount}条')
cursor.close()
db.close()
相关推荐
只会安静敲代码的 小周11 分钟前
AI应用开发
python
FriendshipT16 分钟前
Ultralytics:解读C3Ghost模块
人工智能·pytorch·python·深度学习·目标检测
Java面试题总结21 分钟前
Vue3流式调用大模型接口完整实践
后端·python
宠友信息23 分钟前
从重复请求到订单同步看内容社区源码的处理思路
java·spring boot·redis·后端·mysql·spring
LaughingZhu24 分钟前
Product Hunt 每日热榜 | 2026-07-17
前端·数据库·人工智能·经验分享·mysql·chatgpt·html
怪兽学LLM24 分钟前
AI Agent 记忆系统设计:长短期记忆如何实现?什么时候存?什么时候查?
人工智能·python
DFT计算杂谈27 分钟前
DeepSeek 集群服务器无root本地部署指南
数据库·人工智能·python·opencv·算法
潇凝子潇28 分钟前
UnsupportedOperationException
java·前端·数据库
卷无止境30 分钟前
Python 跨平台 GUI 解决方案全景报告
前端·python
七夜zippoe35 分钟前
DolphinDB实时聚合计算:多维度聚合
数据库·dolphindb