首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >腾讯云云MySQL数据库 维护无法导入CSV文件解决方案

腾讯云云MySQL数据库 维护无法导入CSV文件解决方案

原创
作者头像
用户5247001
发布于 2025-07-30 21:31:35
发布于 2025-07-30 21:31:35
3880
举报

程序是python程序 需要安装python的所需要的库,因为我一直使用CSV文件导入,所以写了个python的简单的程序。

客服说 貌似要维护到9月份。

需要安装的库是:

代码语言:txt
复制
pip install mysql-connector-python

代码语言:txt
复制
import csv
import mysql.connector

# ✅ 数据库连接参数(记得更新 database)
db_config = {
    'host': '',#← 数据库地址
    'port': 28888,#← 数据库端口
    'user': '',#← 数据库用户名
    'password': '',#← 数据库密码
    'database': ''  # ← 数据库名
}

# 📂 CSV 文件路径
csv_file_path = r'C:\Users\Administrator\Downloads\1.csv'#← 文件路径

try:
    # 🔗 建立连接
    conn = mysql.connector.connect(**db_config)
    cursor = conn.cursor()

    # 📖 读取 CSV 文件,自动处理 BOM
    with open(csv_file_path, mode='r', encoding='utf-8-sig') as f:
        reader = csv.reader(f)
        headers = next(reader)

        # ✨ 构建批量插入语句
        columns = ', '.join(headers)
        placeholders = ', '.join(['%s'] * len(headers))
        sql = f"INSERT INTO xxx ({columns}) VALUES ({placeholders})"#← xxx是表名
        rows = list(reader)

        # 🚀 执行批量写入
        cursor.executemany(sql, rows)
        conn.commit()

        print(f"✅ 成功导入 {len(rows)} 条数据到表 !")

except Exception as e:
    print("⚠️ 导入过程出错:", e)

finally:
    # ✅ 关闭连接
    if 'cursor' in locals(): cursor.close()
    if 'conn' in locals(): conn.close()

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档