Python 如何在通过库sqlalchemy循环时执行SQL。不要结束这个问题,因为我找不到任何关于这个问题的答案
我正在尝试通过python执行sql查询。这是基于我的第一个问题,关于如何基于两个数据帧(公司扩展和公司名称的拆分)拆分列。因为我发现用python编写代码很困难,所以我使用sql查询通过python进行拆分和执行。我能够执行诸如update之类的查询,但当我尝试使用循环时,既不会得到输出,也不会出现任何错误 我的意见:Python 如何在通过库sqlalchemy循环时执行SQL。不要结束这个问题,因为我找不到任何关于这个问题的答案,python,pandas,split,Python,Pandas,Split,我正在尝试通过python执行sql查询。这是基于我的第一个问题,关于如何基于两个数据帧(公司扩展和公司名称的拆分)拆分列。因为我发现用python编写代码很困难,所以我使用sql查询通过python进行拆分和执行。我能够执行诸如update之类的查询,但当我尝试使用循环时,既不会得到输出,也不会出现任何错误 我的意见: Original_Input Cleansed_Input
Original_Input Cleansed_Input
DORO INC ( O/S DORO SAFETY & SECURITY) DORO INC OS DORO SAFETY SECURITY
Iris Diagnostics, a Division of Iris International Inc Iris Diagnostics a Division of Iris
International Inc
GINGI-PAK a division of The Belport Co., Inc. GINGIPAK a division of The Belport Co Inc
我的预期产出:
Original_Input Cleansed_Input
DORO INC ( O/S DORO SAFETY & SECURITY) DORO INC OS DORO SAFETY SECURITY
Iris Diagnostics, a Division of Iris International Inc Iris Diagnostics a Division of Iris
International Inc
GINGI-PAK a division of The Belport Co., Inc. GINGIPAK a division of The Belport Co Inc
Core_Input Type_Input
Iris Diagnostics a Division of Iris International Incorporated
GINGIPAK a division of The Belport Company Incorporated
当我尝试使用update语句时,它工作得很好,但是分割有一个优先级,所以我尝试使用while循环并通过python执行,但没有成功。它在sql中运行良好,但不是通过python
在代码Tempcompanyname中,是我的带有输入和输出的表。Company_Extension是另一个带有扩展名、缩写和优先级的表
我的第二张桌子如下图所示
Name_Extension Company_Type Priority
co llc Company LLC 2
Pvt ltd Private Limited 8
Corp orporation 4
Co inc Company Incorporated 9
这是我的代码
engine.execute('''Declare @priority int = (select max(priority) from [company_Extension])
Declare @p int=1
while @p <= @priority
begin
update A
set A.Type_input = B.Company_Type
from [TempCompanyName]A (nolock), [company_Extension]B where A.Cleansed_Input like
'%'+B.Name_Extension and Priority=@p
update A
set A.Core_Input =replace(A.[Cleansed_Input],B.Name_Extension,'')
from [TempCompanyName]A (nolock), [company_Extension]B where A.Cleansed_Input like
'%'+B.Name_Extension and Priority=@p
set @p=@p+1
end''')
engine.execution_options(autocommit=True)
engine.execute(“”“Declare@priority int=(从[company\u Extension]中选择max(priority))
声明@p int=1
虽然@p你已经在这里问了这个问题:这个问题回答了你的问题吗?是的,我已经问了这个问题,但说它已经结束了。我还没有找到这个问题的答案。你仍然在这里问了这个问题:这个问题回答了你的问题吗?是的,我已经问了这个问题,但说它已经结束了。我没有找到这个问题的答案陆上通信线