Python 编写正确的sql炼金术查询的正确顺序是什么?

Python 编写正确的sql炼金术查询的正确顺序是什么?,python,pandas,sqlalchemy,Python,Pandas,Sqlalchemy,我正试图通过excel文件更新我的数据库,但我在运行应用程序时发现此错误,我认为查询语法中存在问题,我不确定sqlAlchemy查询的正确语法是什么,因为我没有找到该语法的参考 下面是如下错误: Traceback (most recent call last): File "D:/Tooling/20200206/MyDB_Update.py", line 21, in <module> if(Add_To_DB.remove_duplicates(item,myCur

我正试图通过excel文件更新我的数据库,但我在运行应用程序时发现此错误,我认为查询语法中存在问题,我不确定sqlAlchemy查询的正确语法是什么,因为我没有找到该语法的参考

下面是如下错误:

Traceback (most recent call last):
  File "D:/Tooling/20200206/MyDB_Update.py", line 21, in <module>
    if(Add_To_DB.remove_duplicates(item,myCursor, myDB)):
  File "D:\Tooling\20200206\MyDB.py", line 22, in remove_duplicates
    mycursor.execute(sql, value)
pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near ','. (102) (SQLExecDirectW); [42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Statement(s) could not be prepared. (8180)")

Process finished with exit code 1
SELECT *
      ,[Date]
      ,[GBSC]
      ,[BSCIndex]
      ,[SiteName]
      ,[CellCI]
      ,[CellLAC]
      ,[CellName]
      ,[CellIndex]
      ,[SiteName.1]
      ,[Voice_SetupFailRate_2G]
      ,[Voice_DropRate_2G]
      ,[OutgHandover_SuccesRate_2G]
      ,[Packet_SetupFailRate_2G]
      ,[Voice_ErlHr_2G]
      ,[Voice_ErlHr_2G_Daily]
      ,[Packet_DLMBytes_2G]
      ,[Packet_ULMBytes_2G]
      ,[Availability_2G]
      ,[Availability_2G_Cluster]
      ,[K3001:FailedSDCCHSeizuresduetoBusySDCCH]
      ,[K3000:SDCCHSeizureRequests]
      ,[CM30:CallDropsonSDCCH]
      ,[CA3030J:CallSetupIndications(SDCCH)(CircuitService)]
      ,[CA313:SuccessfulAssignments]
      ,[CA310:AssignmentRequests]
      ,[CM33:CallDropsonTrafficChannel]
      ,[CM3302:CallDropsonTrafficChannelinStableState(ReleaseIndication)]
      ,[A03701:NormalSpeechDisconnections]
      ,[A9103:NumberofFailedDownlinkGPRSTBFEstablishmentsduetoNoChannel]
      ,[A9303:NumberofFailedDownlinkEGPRSTBFEstablishmentsduetoNoChannel]
      ,[A9104:NumberofFailedDownlinkGPRSTBFEstablishmentsduetoMSNoResponse]
      ,[A9304:NumberofFailedDownlinkEGPRSTBFEstablishmentsduetoMSNoResponse]
      ,[A9101:NumberofDownlinkGPRSTBFEstablishmentAttempts]
      ,[A9301:NumberofDownlinkEGPRSTBFEstablishmentAttempts]
      ,[CH313:NumberofSuccessfulOutgoingInternalInter-CellHandovers]
      ,[CH333:SuccessfulOutgoingExternalInter-CellHandovers]
      ,[CH311:NumberofOutgoingInternalInter-CellHandoverCommands]
      ,[CH331:OutgoingExternalInter-CellHandoverCommands]
      ,[K3014:TrafficVolumeonTCH(Erl)]
      ,[TR373:CellAvailability(%)]
      ,[K3011A:FailedTCHSeizuresduetoBusyTCH(TrafficChannel)]
      ,[K3011B:FailedTCHSeizuresinTCHHandoversduetoBusyTCH(TrafficChannel)]
      ,[K3034:TCHHTrafficVolume(Erl)]
      ,[K3004:TrafficVolumeonSDCCH(Erl)]
      ,[AR9311:AverageNumberofOccupiedPDCHs]
      ,[K3010A:TCHSeizureRequests(TrafficChannel)]
      ,[CM30C:CallDropsonSDCCH(CallType)]
      ,[ZTR104B:CallDropRateonSDCCH(CallType)(%)]
      ,[K3003A:SuccessfulSDCCHSeizures(CallType)]
      ,[K3005:AvailableSDCCHs]
      ,[K3006:ConfiguredSDCCHs]
      ,[K3016:ConfiguredTCHs]
      ,[K3015:AvailableTCHs]
      ,[TCHTrafficVolumeperChannel(Erl)]
      ,[M3030A:CallDropsonTCH(TA)]
      ,[M3030B:CallDropsonTCH(UplinkReceivedLevel)]
      ,[M3030C:CallDropsonTCH(DownlinkReceivedLevel)]
      ,[M3030D:CallDropsonTCH(UplinkandDownlinkReceivedLevel)]
      ,[M3030E:CallDropsonTCH(UplinkFER)]
      ,[M3030F:CallDropsonTCH(DownlinkFER)]
      ,[M3030G:CallDropsonTCH(UplinkandDownlinkFER)]
      ,[M3030H:CallDropsonTCH(UplinkQuality)]
      ,[M3030I:CallDropsonTCH(DownlinkQuality)]
      ,[M3030J:CallDropsonTCH(UplinkandDownlinkQuality)]
      ,[M3030K:CallDropsonTCH(Other)]
      ,[CM331:CallDropsonRadioInterfaceinHandoverState(TrafficChannel)]
      ,[CM332:CallDropsDuetoNoMRfromMSforaLongTime(TrafficChannel)]
      ,[CM333:CallDropsduetoAbisTerrestrialLinkFailure(TrafficChannel)]
      ,[CM334:CallDropsduetoEquipmentFailure(TrafficChannel)]
      ,[CM335:CallDropsduetoForcedHandover(TrafficChannel)]
      ,[CM337:CallDropsafterAnswer]
      ,[CM338:SuccessfulConnections]
      ,[CM339:ClearRequestsSentontheAinterface(TCH)(TrafficChannel)]
      ,[M3020A:CallDropsonSDCCH(TA)]
      ,[M3020B:CallDropsonSDCCH(ReceivedLevel)]
      ,[M3020C:CallDropsonSDCCH(Quality)]
      ,[M3020D:CallDropsonSDCCH(Other)]
  FROM [myDB].[dbo].[mnm_rotterdam_5_daily_details-20191216081027]
这是我的更新功能代码
.py

from sqlalchemy import create_engine


class Add_To_DB():
    # connect db
    mydb = create_engine('mssql+pyodbc://WWX542337CDCD\SMARTRNO_EXPRESS/myDB?driver=SQL+Server+Native+Client+11.0')
    mycursor = mydb.raw_connection().cursor()

    def remove_duplicates(comp_zip, mycursor, mydb):
        # fetching the column from database

        flag = False
        sql = "SELECT GBSC,SiteName,CellCI,CellLAC,CellName,CellIndex FROM [myDB].[dbo].[mnm_rotterdam_5_daily_details-20191216081027] WHERE (GBSC=? and SiteName=?, CellCI =? ,CellLAC=?,CellName=?,CellIndex=?); "
        GBSC = comp_zip[0]
        Site_Name = comp_zip[1],
        Cell_CI = comp_zip[2],
        cell_LAC = comp_zip[3]
        cell_name = comp_zip[4],
        cell_index = comp_zip[5],

        value = (GBSC, Site_Name, Cell_CI, cell_LAC, cell_name, cell_index)
        mycursor.execute(sql, value)
        myresult = mycursor.fetchall()

        if (myresult):
            # print("title URL already exists")
            flag = True
            return (flag)
        else:
            # print("title URL dosen't exist")
            return (flag)

    def insert_into_db(insert_zip, mycursor, mydb):
        sql = "Insert into [myDB].[dbo].[mnm_rotterdam_5_daily_details-20191216081027] (GBSC,SiteName,CellCI,CellLAC,CellName,CellIndex) values (?,?,?,?,?,?)"
        GBSC = insert_zip[0]
        Site_Name = insert_zip[1],
        Cell_CI = insert_zip[2],
        cell_LAC = insert_zip[3]
        cell_name = insert_zip[4],
        cell_index = insert_zip[5],

        value = (GBSC, Site_Name, Cell_CI, cell_LAC, cell_name, cell_index)
        mycursor.execute(sql, value)
        return 'Added'

from sqlalchemy import create_engine
import pandas as pd
from DB import Add_To_DB
mydb = create_engine(
    'mssql+pyodbc://WWX542337CDCD\SMARTRNO_EXPRESS/myDB?driver=SQL+Server+Native+Client+11.0')  # name of database
mycursor = mydb.raw_connection().cursor()
myDB = Add_To_DB.mydb
myCursor = Add_To_DB.mycursor
df = pd.read_excel('MNM_Rotterdam_5_Daily_Details-20191216081027.xlsx',sheet_name='GSM Details')
print(df)
date=df['Date']
GBSC=df['GBSC']
Site_Name=df['Site Name']
Cell_CI=df['Cell CI']
cell_LAC=df['Cell LAC']
cell_name=df['Cell Name']
cell_index=df['CellIndex']

comp_zip=zip(GBSC,Site_Name,Cell_CI,cell_LAC,cell_name,cell_index)
for item in comp_zip:
    if(Add_To_DB.remove_duplicates(item,myCursor, myDB)):
        continue#if data already then flag is true and not insert into db
    Add_To_DB.insert_into_db(item,myCursor, myDB)
#     ##write insert function here
这是我的插入查询:

sql = "Insert into [myDB].[dbo].[mnm_rotterdam_5_daily_details-20191216081027] (GBSC,SiteName,CellCI,CellLAC,CellName,CellIndex) values (?,?,?,?,?,?)"
这是我的选择查询

        sql = "SELECT GBSC,SiteName,CellCI,CellLAC,CellName,CellIndex FROM [myDB].[dbo].[mnm_rotterdam_5_daily_details-20191216081027] WHERE (GBSC=? and SiteName=?, CellCI =? ,CellLAC=?,CellName=?,CellIndex=?); "
这是我的DB查询,如下所示:

Traceback (most recent call last):
  File "D:/Tooling/20200206/MyDB_Update.py", line 21, in <module>
    if(Add_To_DB.remove_duplicates(item,myCursor, myDB)):
  File "D:\Tooling\20200206\MyDB.py", line 22, in remove_duplicates
    mycursor.execute(sql, value)
pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near ','. (102) (SQLExecDirectW); [42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Statement(s) could not be prepared. (8180)")

Process finished with exit code 1
SELECT *
      ,[Date]
      ,[GBSC]
      ,[BSCIndex]
      ,[SiteName]
      ,[CellCI]
      ,[CellLAC]
      ,[CellName]
      ,[CellIndex]
      ,[SiteName.1]
      ,[Voice_SetupFailRate_2G]
      ,[Voice_DropRate_2G]
      ,[OutgHandover_SuccesRate_2G]
      ,[Packet_SetupFailRate_2G]
      ,[Voice_ErlHr_2G]
      ,[Voice_ErlHr_2G_Daily]
      ,[Packet_DLMBytes_2G]
      ,[Packet_ULMBytes_2G]
      ,[Availability_2G]
      ,[Availability_2G_Cluster]
      ,[K3001:FailedSDCCHSeizuresduetoBusySDCCH]
      ,[K3000:SDCCHSeizureRequests]
      ,[CM30:CallDropsonSDCCH]
      ,[CA3030J:CallSetupIndications(SDCCH)(CircuitService)]
      ,[CA313:SuccessfulAssignments]
      ,[CA310:AssignmentRequests]
      ,[CM33:CallDropsonTrafficChannel]
      ,[CM3302:CallDropsonTrafficChannelinStableState(ReleaseIndication)]
      ,[A03701:NormalSpeechDisconnections]
      ,[A9103:NumberofFailedDownlinkGPRSTBFEstablishmentsduetoNoChannel]
      ,[A9303:NumberofFailedDownlinkEGPRSTBFEstablishmentsduetoNoChannel]
      ,[A9104:NumberofFailedDownlinkGPRSTBFEstablishmentsduetoMSNoResponse]
      ,[A9304:NumberofFailedDownlinkEGPRSTBFEstablishmentsduetoMSNoResponse]
      ,[A9101:NumberofDownlinkGPRSTBFEstablishmentAttempts]
      ,[A9301:NumberofDownlinkEGPRSTBFEstablishmentAttempts]
      ,[CH313:NumberofSuccessfulOutgoingInternalInter-CellHandovers]
      ,[CH333:SuccessfulOutgoingExternalInter-CellHandovers]
      ,[CH311:NumberofOutgoingInternalInter-CellHandoverCommands]
      ,[CH331:OutgoingExternalInter-CellHandoverCommands]
      ,[K3014:TrafficVolumeonTCH(Erl)]
      ,[TR373:CellAvailability(%)]
      ,[K3011A:FailedTCHSeizuresduetoBusyTCH(TrafficChannel)]
      ,[K3011B:FailedTCHSeizuresinTCHHandoversduetoBusyTCH(TrafficChannel)]
      ,[K3034:TCHHTrafficVolume(Erl)]
      ,[K3004:TrafficVolumeonSDCCH(Erl)]
      ,[AR9311:AverageNumberofOccupiedPDCHs]
      ,[K3010A:TCHSeizureRequests(TrafficChannel)]
      ,[CM30C:CallDropsonSDCCH(CallType)]
      ,[ZTR104B:CallDropRateonSDCCH(CallType)(%)]
      ,[K3003A:SuccessfulSDCCHSeizures(CallType)]
      ,[K3005:AvailableSDCCHs]
      ,[K3006:ConfiguredSDCCHs]
      ,[K3016:ConfiguredTCHs]
      ,[K3015:AvailableTCHs]
      ,[TCHTrafficVolumeperChannel(Erl)]
      ,[M3030A:CallDropsonTCH(TA)]
      ,[M3030B:CallDropsonTCH(UplinkReceivedLevel)]
      ,[M3030C:CallDropsonTCH(DownlinkReceivedLevel)]
      ,[M3030D:CallDropsonTCH(UplinkandDownlinkReceivedLevel)]
      ,[M3030E:CallDropsonTCH(UplinkFER)]
      ,[M3030F:CallDropsonTCH(DownlinkFER)]
      ,[M3030G:CallDropsonTCH(UplinkandDownlinkFER)]
      ,[M3030H:CallDropsonTCH(UplinkQuality)]
      ,[M3030I:CallDropsonTCH(DownlinkQuality)]
      ,[M3030J:CallDropsonTCH(UplinkandDownlinkQuality)]
      ,[M3030K:CallDropsonTCH(Other)]
      ,[CM331:CallDropsonRadioInterfaceinHandoverState(TrafficChannel)]
      ,[CM332:CallDropsDuetoNoMRfromMSforaLongTime(TrafficChannel)]
      ,[CM333:CallDropsduetoAbisTerrestrialLinkFailure(TrafficChannel)]
      ,[CM334:CallDropsduetoEquipmentFailure(TrafficChannel)]
      ,[CM335:CallDropsduetoForcedHandover(TrafficChannel)]
      ,[CM337:CallDropsafterAnswer]
      ,[CM338:SuccessfulConnections]
      ,[CM339:ClearRequestsSentontheAinterface(TCH)(TrafficChannel)]
      ,[M3020A:CallDropsonSDCCH(TA)]
      ,[M3020B:CallDropsonSDCCH(ReceivedLevel)]
      ,[M3020C:CallDropsonSDCCH(Quality)]
      ,[M3020D:CallDropsonSDCCH(Other)]
  FROM [myDB].[dbo].[mnm_rotterdam_5_daily_details-20191216081027]

有人知道这个错误吗?

Site\u Name=comp\u zip[1],
等中。您已经为
Site\u Name
分配了一个1元组。删除尾随的逗号。@Ilja Everilä非常感谢它解决了这个问题,但我发现了另一个语法错误:)
pyodbc.ProgrammingError:('42000',“[42000][Microsoft][SQL Server Native Client 11.0][SQL Server]靠近','的语法不正确。(102)(SQLExecDirectW);[42000][Microsoft][SQL Server Native Client 11.0][SQL Server]语句无法准备。(8180)”)
其中(GBSC=?和SiteName=?,CellCI=?,CellLAC=?,CellName=?,CellIndex=?)
似乎有点奇怪。括号似乎不必要,而不是逗号,您的意思可能是
?可能类似于
WHERE GBSC=?(SiteName=?或CellCI=?或CellLAC=?或CellName=?或CellIndex=?)
(只是想猜测一下原意)@Ilja Everilä非常感谢它解决了。。。但是当我检查它是否更新数据库时,还有一个问题,我看到它在sql中没有任何更新。。。您知道如何创建一个函数来打印输出以确保函数正常工作吗?:)