You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:pyodbc execute正常但executemany无法向Teradata写入数据

问题原因及解决方法

核心问题:executemany参数格式错误

cursor.executemany()的第二个参数要求是二维序列结构(比如列表的列表、元组的元组),代表多条记录的参数集合。你当前的写法是把单条记录的每个字段作为独立参数传入,不符合方法的参数要求,导致数据库未接收到有效插入数据,自然不会写入。

修复步骤

1. 修正executemany的参数格式

把单条row的字段包装成嵌套序列,比如:

cursor.executemany(
    "INSERT INTO Table(Cal_yr_num, Cak_pd_num, Banner, Company_code, Document_Number, GL_Account, Text_Info, Cheque_No, General_Ledger_Amount, Profit_Center, Cost_Center, CoCode_ChqNo, Account_Name) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
    [(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12])]
)

或者利用pandas Series的tolist()方法简化:

cursor.executemany(
    "INSERT INTO Table(Cal_yr_num, Cak_pd_num, Banner, Company_code, Document_Number, GL_Account, Text_Info, Cheque_No, General_Ledger_Amount, Profit_Center, Cost_Center, CoCode_ChqNo, Account_Name) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
    [row.tolist()]
)

2. 优化批量插入效率(真正发挥executemany的作用)

当前循环每行调用executemany和用execute效率无差,建议按批次处理数据(比如每1000行一批):

import pyodbc
import pandas as pd  # 修正导入别名问题

df = pd.read_excel(Srpath, sheet_name="Sheet1")
batch_size = 1000  # 可根据数据库性能调整

connection = pyodbc.connect(r"Driver={Teradata Database ODBC Driver 16.20};DBCNAME=TDPROD1;AUTHENTICATION=LDAP;UID=" + username + ";PWD=" + password)
cursor = connection.cursor()

# 按批次拆分数据
for i in range(0, len(df), batch_size):
    batch_df = df.iloc[i:i+batch_size]
    # 把批次数据转成二维列表
    params = batch_df.values.tolist()
    cursor.executemany(
        "INSERT INTO Table(Cal_yr_num, Cak_pd_num, Banner, Company_code, Document_Number, GL_Account, Text_Info, Cheque_No, General_Ledger_Amount, Profit_Center, Cost_Center, CoCode_ChqNo, Account_Name) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
        params
    )
    connection.commit()  # 每批次提交一次

cursor.close()
connection.close()

3. 其他小问题修正

  • 导入语句需改为import pandas as pd,否则pd.read_excel会报错(你说未触发报错,可能实际代码已修正,但给出的代码存在此问题)。
  • SQL语句中第10个占位符后多了一个逗号(原写法?, ?, ?, ?, ?, ?, ?, ?, ?,?, ?, ?, ?),需修正为?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,避免语法错误。

内容的提问来源于stack exchange,提问作者wintersoul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 18:12:49