gpt4 book ai didi

python - 通过 python 将 csv 文件插入 MySQL。运行但数据未填充到表中

转载 作者:行者123 更新时间:2023-12-04 04:10:49 33 4
gpt4 key购买 nike

首先,我对 python 很陌生,所以请多多关照。我正在尝试获取一个 CSV 文件,该文件是从位于我的驱动器上的文件夹中的 SQL 查询生成的,并通过 python 将该文件上的数据“上传”/插入到 MySQL 表中。我不想简单地手动上传文件。该脚本运行并显示数据处理,但是当我进入表格并尝试搜索它时,什么也没有出现。下面是我写的:

cursor = cnx.cursor()

if cnx.is_connected():
print('Successfully connected to MySQL database')

cursor.execute("create database if not exists Character_Stats;")

cursor.execute("use Character_Stats;")

DB_NAME = 'Character_Stats_DB'
TABLES = {}
cursor.execute('DROP TABLE IF EXISTS Local_Players')

cursor.execute("""CREATE TABLE IF NOT EXISTS `Local_Players` (
`game_account_id` int,
`character_key` char(20) DEFAULT NULL,
`game_region_key` int(20) DEFAULT NULL,
`character_id` int(20) DEFAULT NULL,
`class_name` TEXT(20) DEFAULT NULL,
`specialization_id` int(20) DEFAULT NULL,
`character_name` TEXT(20) DEFAULT NULL,
`name` TEXT DEFAULT NULL,
`character_level` int(20) DEFAULT NULL,
`total_dmg` int(20) DEFAULT NULL,
`avg_dmg_per_game` int(20) DEFAULT NULL,
`total_healing` int(20) DEFAULT NULL,
`total_dmg_absorbed` int(20) DEFAULT NULL,
`total_heal_absorbed` int(20) DEFAULT NULL,
`avg_heal_absorbed_per_game` int(20) DEFAULT NULL,
`total_killing_blows` int(20) DEFAULT NULL,
`avg_killing_blows_per_game` int(20) DEFAULT NULL,
`total_deaths` int(20) DEFAULT NULL,
`avg_deaths_per_game` int(20) DEFAULT NULL,
`total_time_hrs` int(20) DEFAULT NULL,
`total_games_played` int(20) DEFAULT NULL,
PRIMARY KEY (game_account_id))
ENGINE = InnoDB""")

cnx.commit()

for data in query:
with open('Player_stats.csv', 'r', encoding="utf-8", newline='') as playerdata:
csv_reader = csv.reader(playerdata, delimiter=',')
for row in csv_reader:
print(row)
character_key = row[0]
game_region_key = row[1]
character_id = row[2]
class_name = row[3]
character_name = row[4]
realm_key = row[5]
name = row[6]
character_level = row[7]
total_dmg = row[8]
avg_dmg_per_game = row[9]
total_healing = row[10]
avg_healing_per_game = row[11]
total_dmg_absorbed = row[12]
total_heal_absorbed = row[13]
avg_heal_absorbed_per_game = row[14]
total_killing_blows = row[15]
avg_killing_blows_per_game = row[16]
total_deaths = row[17]
avg_deaths_per_game = row[18]
total_time_hrs = row[19]
total_games_played = row[20]
cursor.execute('''INSERT INTO Character_Stats.Local_Players(character_key,game_region_key
, character_id, class_name, character_name, realm_key, name, character_level, total_dmg, avg_dmg_per_game,
total_healing, avg_healing_per_game, total_dmg_absorbed, total_heal_absorbed, avg_heal_absorbed_per_game,
total_killing_blows, avg_killing_blows_per_game, total_deaths, avg_deaths_per_game, total_time_hrs, total_games_played),
VALUES(%s,%s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s)''', multi=True)
print(data)


print("Data loading complete. \n")

如有任何帮助,我们将不胜感激。

这是我的 CSV 的输出:

enter image description here

最佳答案

此代码将使用您的 csv 文件填充表格中的所有列。只需尝试阅读这些模块的文档,因为 Python 是一种非常智能和简单的语言,带有库:

安装 pandas、pymsql 和 sqlalchemy:

pip install sqlalchemy
pip install pymysql
pip install pandas

试试这个:

import pandas as pd
from sqlalchemy import create_engine
connection_string = "mysql+pymysql://%s:%s@%s:%s/%s" % ('user_name','ur_connection_password' , 'host', PORT, 'schema_name')
engine = create_engine(connection_string)
connection = engine.connect()
connection.execute("""
CREATE TABLE IF NOT EXISTS `Local_Players` (
`game_account_id` int NOT NULL AUTO_INCREMENT,
`character_key` varchar(20) DEFAULT NULL,
`game_region_key` int(20) DEFAULT NULL,
`character_id` int(20) DEFAULT NULL,
`class_name` TEXT(20) DEFAULT NULL,
`specialization_id` int(20) DEFAULT NULL,
`character_name` TEXT(20) DEFAULT NULL,
`name` TEXT DEFAULT NULL,
`character_level` int(20) DEFAULT NULL,
`total_dmg` int(20) DEFAULT NULL,
`avg_dmg_per_game` int(20) DEFAULT NULL,
`total_healing` int(20) DEFAULT NULL,
`total_dmg_absorbed` int(20) DEFAULT NULL,
`total_heal_absorbed` int(20) DEFAULT NULL,
`avg_heal_absorbed_per_game` int(20) DEFAULT NULL,
`total_killing_blows` int(20) DEFAULT NULL,
`avg_killing_blows_per_game` int(20) DEFAULT NULL,
`total_deaths` int(20) DEFAULT NULL,
`avg_deaths_per_game` int(20) DEFAULT NULL,
`total_time_hrs` int(20) DEFAULT NULL,
`total_games_played` int(20) DEFAULT NULL,
PRIMARY KEY (`game_account_id`))""")
pd.read_sql_table(table_name='Local_Players',schema='Test_DB', con=engine)
df = pd.read_csv(r"Player_stats.csv")
df.to_sql(name="Local_Players", if_exists='append', chunksize=1000, con=engine, index=False)

关于python - 通过 python 将 csv 文件插入 MySQL。运行但数据未填充到表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61760721/

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