我正在从Python运行一个相当完善的MS Access查询:
qry = '''
UPDATE H500_ODFlows INNER JOIN H500_UPDATE ON
(H500_ODFlows.Product = H500_UPDATE.Product)
AND (H500_ODFlows.Dest = H500_UPDATE.DestCode)
AND (H500_ODFlows.Orig = H500_UPDATE.OrigCode)
SET H500_ODFlows.Pieces = [H500_UPDATE].[Pieces],
H500_ODFlows.Weight = [H500_UPDATE].[Weight],
H500_ODFlows.Cons = [H500_UPDATE].[Pieces],
H500_ODFlows.DeadWeight = [H500_UPDATE].[DeadWeight],
H500_ODFlows.DoNotRead = [H500_UPDATE].DoNotRead,
H500_ODFlows.[_OrigCountryCode] = [H500_UPDATE].[_OrigCountryCode],
H500_ODFlows.[_DestCountryCode] = [H500_UPDATE].[_DestCountryCode]
'''
try:
crsr.execute(lb.cleanqry(qry))
cnxn.commit()
print('Updating was successful.')
except Exception as err:
print('Updating failed. See the error.' + str(err))
但出现以下错误:
(''HY000','[HY000] [Microsoft] [ODBC Microsoft Access驱动程序]文件 超过共享锁计数。增加MaxLocksPerFile注册表项。 (-1033)(SQLExecDirectW)')
我按照说明增加了“ MaxLocksPerFile”,但没有帮助。此外,查询可以在MS Access中运行,但不能通过Python运行。有什么建议吗?
答案 0 :(得分:1)
尝试使用autocommit on运行查询。这样,数据库将不需要保持所有这些锁处于打开状态,而只需在查询运行时提交所有内容即可。
qry = '''
UPDATE H500_ODFlows INNER JOIN H500_UPDATE ON
(H500_ODFlows.Product = H500_UPDATE.Product)
AND (H500_ODFlows.Dest = H500_UPDATE.DestCode)
AND (H500_ODFlows.Orig = H500_UPDATE.OrigCode)
SET H500_ODFlows.Pieces = [H500_UPDATE].[Pieces],
H500_ODFlows.Weight = [H500_UPDATE].[Weight],
H500_ODFlows.Cons = [H500_UPDATE].[Pieces],
H500_ODFlows.DeadWeight = [H500_UPDATE].[DeadWeight],
H500_ODFlows.DoNotRead = [H500_UPDATE].DoNotRead,
H500_ODFlows.[_OrigCountryCode] = [H500_UPDATE].[_OrigCountryCode],
H500_ODFlows.[_DestCountryCode] = [H500_UPDATE].[_DestCountryCode]
'''
try:
cnxn.autocommit = True
crsr.execute(lb.cleanqry(qry))
print('Updating was successful.')
except Exception as err:
print('Updating failed. See the error.' + str(err))
答案 1 :(得分:1)
自从您注意到:该查询在MS Access中运行得很好,但不能通过Python运行。一个可能的原因是Access存储的查询比称为查询的应用程序层更有效,因为引擎可以保存和缓存最佳执行计划。在处理字符串SQL语句的应用程序层(Python,VBA等)中,Jet / ACE引擎没有时间计划最佳执行。
因此,请考虑以下因素:
JOIN
变量中。UPDATE
查询另存为数据库内部的存储查询。在这里,保存过程将检查语法,计算和优化计划以及缓存统计信息。 然后,使用CALL
命令在Python中将查询作为存储的proc运行
# SET AUTOCOMMIT PREFERENCE IN CONNECTION
cnxn = pyodbc.connect(..., autocommit=True)
...
crsr.execute("{CALL myUpdateQuery}")