2

我的存储过程本身可以正常工作,但是我的 python 脚本无法使用我下载的文件完全执行存储过程。python脚本的目的是使用ftp下载文件并将文件存储在本地。它首先比较远程位置和本地位置以找到新文件,然后将新文件下载到本地位置。然后对每个新文件执行存储过程。

蟒蛇脚本:

import os
import ftplib
import pyodbc

连接到 sql server* conn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;DATABASE=Development;UID=myid;PWD=mypassword') cursor = conn.cursor()

ftp = ftplib.FTP("myftpaddress.com")
ftp.login("loginname", "password")
print 'ftp on'

#directory listing 
rfiles = ftp.nlst()
print 'remote listing'

#save local directory listing to files
lfiles = os.listdir(r"D:\Raw_Data\myFiles")
print 'local listing'

#compare and find files in rfiles but not in lfiles
nfiles = set(rfiles) - set(lfiles)
nfiles = list(nfiles)
print 'compared listings'

#loop through the new files
#download the new files and open each file and run stored proc
#close files and disconnect to sql server
for n in nfiles:
   local_filename = os.path.join(r"D:\Raw_Data\myFiles",n)
   lf = open(local_filename, "wb")
   ftp.retrbinary("RETR " + n, lf.write, 1024)
   lf.close()
   print 'file written'
  cursor.execute("exec SP_my_Dailyfiles('n')")
  conn.close()
  lf.close()
  print 'sql executed'


ftp.quit()

存储过程:

ALTER PROCEDURE [dbo].[SP_my_Dailyfiles] 
    -- Add the parameters for the stored procedure here
@file VARCHAR(255)
-- Add the parameters for the stored procedure here

AS
BEGIN


IF EXISTS(SELECT * FROM sysobjects WHERE name = 'myinvoice')
DROP TABLE dbo.myinvoice
----------------------------------------------------------------------------------------------------
CREATE TABLE myinvoice(

     [Billing] varchar(255)
    ,[Order] varchar(45)
    ,[Item] varchar(255)
    ,[Quantity in pack] varchar(255)
    ,[Invoice] varchar(255)
    ,[Date] varchar(255)
    ,[Cost] varchar(255)
    ,[Quantity of pack] varchar(255)
    ,[Extended] varchar(255)
    ,[Type] varchar(25)
    ,[Date Due] varchar(255)

)

----------------------------------------------------------------------------------------------------            
DECLARE @SourceDirectory VARCHAR(255)
DECLARE @SourceFile VARCHAR(255)



EXEC (' BULK
    INSERT dbo.myinvoice 
    FROM ''D:\Raw_Data\myfile\'+@file+'''
    WITH
    (
        FIRSTROW = 1,
        FIELDTERMINATOR = '','',
        ROWTERMINATOR = ''0x0a''
    )'
)
-------------------------------------------------------------------------------------------------------------


INSERT INTO [Development].[dbo].[my_Dailyfiles](

     [Billing]
    ,[Order] 
    ,[Item] 
    ,[Quantity in pack] 
    ,[Invoice] 
    ,[Date]  
    ,[Cost] 
    ,[Quantity of pack] 
    ,[Extended] 
    ,[Type] 
    ,[Date Due] 
    ,[FileName] 
    ,[IMPORTEDDATE] 

    )
    SELECT

         replace([Billing], '"', '') 
          ,replace([Order], '"', '') 
          ,replace([Item], '"','') 
          ,replace([Quantity in pack],'"','') 
          ,replace([Invoice],'"','') 
          ,cast(replace([Date],'"','') as varchar(255)) as date
          ,replace([Cost],'"','')
          ,replace([Quantity of pack],'"','') 
          ,replace([Extended],'"','') 
          ,replace([Type],'"','') 
          ,cast(replace([Date Due],'"','') as varchar(255)) as date
          ,@file,
          GetDate()      


 FROM [myinvoice] WHERE [Bill to] <> ' '  and ndc != '"***********"'
4

1 回答 1

1

我认为问题可能是您在执行存储过程后立即关闭数据库连接,同时仍在循环中。

这意味着第二次循环时,当您尝试执行 SP 时,DB 连接将关闭。我实际上希望在循环周围第二次抛出错误。

我的结构是这样的:

conn = pyodbc.connect(...)
for n in nfiles:
    ...
    cursor = conn.cursor()
    cursor.execute("exec SP_my_Dailyfiles('n')")
    conn.commit()
于 2012-12-03T15:28:51.457 回答