gpt4 book ai didi

python - 如何使用 PyMySQL 将 Pandas Dataframe 插入 MySql

转载 作者:行者123 更新时间:2023-11-29 05:01:28 35 4
gpt4 key购买 nike

我有一个 DataFrame,它有大约 30,000 多行和 150 多列。所以,目前我正在使用以下代码将数据插入 MySQL。但是由于它一次读取一行,因此将所有行插入 MySql 会花费太多时间。

有什么方法可以一次或分批插入所有行?这里的约束是我只需要使用 PyMySQL,我不能安装任何其他库。

import pymysql
import pandas as pd

# Create dataframe
data = pd.DataFrame({
'book_id':[12345, 12346, 12347],
'title':['Python Programming', 'Learn MySQL', 'Data Science Cookbook'],
'price':[29, 23, 27]
})


# Connect to the database
connection = pymysql.connect(host='localhost',
user='root',
password='12345',
db='book')


# create cursor
cursor=connection.cursor()

# creating column list for insertion
cols = "`,`".join([str(i) for i in data.columns.tolist()])

# Insert DataFrame recrds one by one.
for i,row in data.iterrows():
sql = "INSERT INTO `book_details` (`" +cols + "`) VALUES (" + "%s,"*(len(row)-1) + "%s)"
cursor.execute(sql, tuple(row))

# the connection is not autocommitted by default, so we must commit to save our changes
connection.commit()

# Execute query
sql = "SELECT * FROM `book_details`"
cursor.execute(sql)

# Fetch all the records
result = cursor.fetchall()
for i in result:
print(i)

connection.close()

谢谢。

最佳答案

尝试使用 SQLALCHEMY 创建一个引擎,稍后您可以使用 pandas df.to_sql 函数。此函数将行从 pandas 数据帧写入 SQL 数据库,它比迭代数据帧和使用 MySql 游标快得多。

你的代码看起来像这样:

import pymysql
import pandas as pd
from sqlalchemy import create_engine

# Create dataframe
data = pd.DataFrame({
'book_id':[12345, 12346, 12347],
'title':['Python Programming', 'Learn MySQL', 'Data Science Cookbook'],
'price':[29, 23, 27]
})

db_data = 'mysql+mysqldb://' + 'root' + ':' + '12345' + '@' + 'localhost' + ':3306/' \
+ 'book' + '?charset=utf8mb4'
engine = create_engine(db_data)

# Connect to the database
connection = pymysql.connect(host='localhost',
user='root',
password='12345',
db='book')

# create cursor
cursor=connection.cursor()
# Execute the to_sql for writting DF into SQL
data.to_sql('book_details', engine, if_exists='append', index=False)

# Execute query
sql = "SELECT * FROM `book_details`"
cursor.execute(sql)

# Fetch all the records
result = cursor.fetchall()
for i in result:
print(i)

engine.dispose()
connection.close()

您可以查看此函数在 pandas doc 中的所有选项

关于python - 如何使用 PyMySQL 将 Pandas Dataframe 插入 MySql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58232218/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com