准备excel文件,记录要导入的数据;使用Navicat for MySQL连接MySQL数据库;
1、首先,准备excel文件,记录要导入的数据;
2、使用Navicat for MySQL连接MySQL数据库;
3、在Navicat for MySQL中创建一个新的表,用于存放excel中的数据;
4、在Navicat for MySQL中点击“工具”-“数据传输”,打开“数据传输”对话框;
5、在“数据传输”对话框中,选择“从文件导入”,点击“浏览”按钮,选择要导入的excel文件;
6、点击“确定”按钮,将excel中的数据导入到MySQL数据库中。
以下是使用Python代码实现excel导入MySQL数据库的例子:
import xlrd
import pymysql
# 打开excel文件
workbook = xlrd.open_workbook('test.xlsx')
# 获取sheet对象
sheet = workbook.sheet_by_name('Sheet1')
# 连接MySQL数据库
db = pymysql.connect(host='localhost', user='root', password='123456', database='test')
cursor = db.cursor()
# 循环读取excel中的数据,并插入到MySQL数据库中
for i in range(1, sheet.nrows):
# 读取excel中的数据
id = sheet.cell(i, 0).value
name = sheet.cell(i, 1).value
age = sheet.cell(i, 2).value
# 执行SQL语句
sql = "INSERT INTO student (id, name, age) VALUES (%s, %s, %s)"
params = (id, name, age)
cursor.execute(sql, params)
# 提交事务
db.commit()
# 关闭数据库连接
cursor.close()
db.close()
本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处
评论列表(71条)