How to solve for pyodbc.ProgrammingError: The second parameter to executemany must not be empty
How to solve for pyodbc.ProgrammingError: The second parameter to executemany must not be empty
您好,我在将数据从一个数据库传输到另一个数据库时遇到问题。我在 msql db 上使用 table 中的字段创建了一个列表,使用该列表查询和 oracle db table (使用 where 语句中的初始列表来过滤结果)然后我将查询结果加载回 msql db.
程序在前几次迭代中运行,但随后出错,出现以下错误 (
追溯(最近一次通话):
文件“C:/Users/1/PycharmProjects/DataExtracts/BuyerGroup.py”,第 67 行,位于
insertIntoMSDatabase(idString)
insertIntoMSDatabase 中的文件“C:/Users/1/PycharmProjects/DataExtracts/BuyerGroup.py”,第 48 行
mycursor.executemany(sql, 值)
pyodbc.ProgrammingError:executemany的第二个参数不能为空。)
我似乎无法在网上找到解决此错误消息的指导。我觉得这可能是一个简单的解决方案,但我就是做不到...
# import libraries
import cx_Oracle
import pyodbc
import logging
import time
import re
import math
import numpy as np
logging.basicConfig(level=logging.DEBUG)
conn = pyodbc.connect('''Driver={SQL Server Native Client 11.0};
Server='servername';
Database='dbname';
Trusted_connection=yes;''')
b = conn.cursor()
dsn_tns = cx_Oracle.makedsn('Hostname', 'port', service_name='name')
conn1 = cx_Oracle.connect(user=r'uid', password='pwd', dsn=dsn_tns)
c = conn1.cursor()
beginTime = time.time()
bind = (b.execute('''select distinct field1
from [server].[db].[dbo].[table]'''))
print('MSQL table(s) queried, List Generated')
# formats ids for sql string
def surroundWithQuotes(id):
return "'" + re.sub(",|\s$", "", str(id)) + "'"
def insertIntoMSDatabase(idString):
osql = '''SELECT distinct field1, field2
FROM Database.Table
WHERE field2 is not null and field3 IN ({})'''.format(idString)
c.execute(osql)
claimsdata = c.fetchall()
print('Oracle table(s) queried, Data Pulled')
mycursor = conn.cursor()
sql = '''INSERT INTO [dbo].[tablename]
(
[fields1]
,[field2]
)
VALUES (?,?)'''
val = claimsdata
mycursor.executemany(sql, val)
conn.commit()
ids = []
formattedIdStrings = []
# adds all the ids found in bind to an iterable array
for row in bind:
ids.append(row[0])
# splits the ids[] array into multiple arrays < 1000 in length
batchedIds = np.array_split(ids, math.ceil(len(ids) / 1000))
# formats the value inside each batchedId to be a string
for batchedId in batchedIds:
formattedIdStrings.append(",".join(map(surroundWithQuotes, batchedId)))
# runs insert into MS database for each batch of IDs
for idString in formattedIdStrings:
insertIntoMSDatabase(idString)
print("MSQL table loaded, Data inserted into destination")
endTime = time.time()
print("Program Time Elapsed: ",endTime-beginTime)
conn.close()
conn1.close()
mycursor.executemany(sql, val)
pyodbc.ProgrammingError: The second parameter to executemany must not be empty.
在调用 .executemany()
之前,您需要验证 val
不是一个空列表(如果在 SELECT 语句上调用 .fetchall()
就会出现这种情况returns 没有行),例如,
if val:
mycursor.executemany(sql, val)
您好,我在将数据从一个数据库传输到另一个数据库时遇到问题。我在 msql db 上使用 table 中的字段创建了一个列表,使用该列表查询和 oracle db table (使用 where 语句中的初始列表来过滤结果)然后我将查询结果加载回 msql db.
程序在前几次迭代中运行,但随后出错,出现以下错误 ( 追溯(最近一次通话): 文件“C:/Users/1/PycharmProjects/DataExtracts/BuyerGroup.py”,第 67 行,位于 insertIntoMSDatabase(idString) insertIntoMSDatabase 中的文件“C:/Users/1/PycharmProjects/DataExtracts/BuyerGroup.py”,第 48 行 mycursor.executemany(sql, 值) pyodbc.ProgrammingError:executemany的第二个参数不能为空。)
我似乎无法在网上找到解决此错误消息的指导。我觉得这可能是一个简单的解决方案,但我就是做不到...
# import libraries
import cx_Oracle
import pyodbc
import logging
import time
import re
import math
import numpy as np
logging.basicConfig(level=logging.DEBUG)
conn = pyodbc.connect('''Driver={SQL Server Native Client 11.0};
Server='servername';
Database='dbname';
Trusted_connection=yes;''')
b = conn.cursor()
dsn_tns = cx_Oracle.makedsn('Hostname', 'port', service_name='name')
conn1 = cx_Oracle.connect(user=r'uid', password='pwd', dsn=dsn_tns)
c = conn1.cursor()
beginTime = time.time()
bind = (b.execute('''select distinct field1
from [server].[db].[dbo].[table]'''))
print('MSQL table(s) queried, List Generated')
# formats ids for sql string
def surroundWithQuotes(id):
return "'" + re.sub(",|\s$", "", str(id)) + "'"
def insertIntoMSDatabase(idString):
osql = '''SELECT distinct field1, field2
FROM Database.Table
WHERE field2 is not null and field3 IN ({})'''.format(idString)
c.execute(osql)
claimsdata = c.fetchall()
print('Oracle table(s) queried, Data Pulled')
mycursor = conn.cursor()
sql = '''INSERT INTO [dbo].[tablename]
(
[fields1]
,[field2]
)
VALUES (?,?)'''
val = claimsdata
mycursor.executemany(sql, val)
conn.commit()
ids = []
formattedIdStrings = []
# adds all the ids found in bind to an iterable array
for row in bind:
ids.append(row[0])
# splits the ids[] array into multiple arrays < 1000 in length
batchedIds = np.array_split(ids, math.ceil(len(ids) / 1000))
# formats the value inside each batchedId to be a string
for batchedId in batchedIds:
formattedIdStrings.append(",".join(map(surroundWithQuotes, batchedId)))
# runs insert into MS database for each batch of IDs
for idString in formattedIdStrings:
insertIntoMSDatabase(idString)
print("MSQL table loaded, Data inserted into destination")
endTime = time.time()
print("Program Time Elapsed: ",endTime-beginTime)
conn.close()
conn1.close()
mycursor.executemany(sql, val)
pyodbc.ProgrammingError: The second parameter to executemany must not be empty.
在调用 .executemany()
之前,您需要验证 val
不是一个空列表(如果在 SELECT 语句上调用 .fetchall()
就会出现这种情况returns 没有行),例如,
if val:
mycursor.executemany(sql, val)