<p>有几件事</p>
<ol>
<li><p>为什么要迭代ProductInventory两次</p></li>
<li><p><code>executemany</code>调用不应该在构建了整个元组或一批元组之后发生吗</p></li>
<li><p>pyodbc文档指出,“使用fast_executemany=False运行executemany()通常不会比直接运行多个execute()命令快多少。”因此,您需要在这两个示例中设置<code>cursor.fast_executemany=True</code>(有关更多详细信息/示例,请参见<a href="https://github.com/mkleehammer/pyodbc/wiki/Cursor" rel="nofollow noreferrer">https://github.com/mkleehammer/pyodbc/wiki/Cursor</a>)。我不确定为什么在示例2中省略了它</p></li>
</ol>
<p>下面是一个例子,说明你如何完成我认为你正在努力做的事情。<code>math.ceil</code>和<code>end_idx = ...</code>中的条件表达式占最后一批,可能是奇数大小。因此,在下面的示例中,您有10行,批大小为3,因此最终有4个批,最后一个只有1个元组</p>
<pre><code>import math
df = ProductInventory
batch_size = 500
num_batches = math.ceil(len(df)/batch_size)
for i in range(num_batches):
start_idx = i * batch_size
end_idx = len(df) if i + 1 == num_batches else start_idx + batch_size
tuple_of_tuples = tuple(tuple(x) for x in df.iloc[start_idx:end_idx, :].values.tolist())
cursor.executemany("INSERT INTO table ([x]],[Y]) values (?,?)", tuple_of_tuples)
</code></pre>
<p>示例输出:</p>
<pre><code>=== Executing: ===
df = pd.DataFrame({'a': range(1,11), 'b': range(101,111)})
batch_size = 3
num_batches = math.ceil(len(df)/batch_size)
for i in range(num_batches):
start_idx = i * batch_size
end_idx = len(df) if i + 1 == num_batches else start_idx + batch_size
tuple_of_tuples = tuple(tuple(x) for x in df.iloc[start_idx:end_idx, :].values.tolist())
print(tuple_of_tuples)
=== Output: ===
((1, 101), (2, 102), (3, 103))
((4, 104), (5, 105), (6, 106))
((7, 107), (8, 108), (9, 109))
((10, 110),)
</code></pre>