我的python脚本没有完全执行我的存储过程

时间:2012-12-03 15:10:10

标签: python stored-procedures

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

python脚本:

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 != '"***********"'

1 个答案:

答案 0 :(得分:0)

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

这意味着第二次循环,当您尝试执行SP时,数据库连接将关闭。我实际上会期望在循环中抛出第二个错误。

我的结构方式如下:

conn = pyodbc.connect(...)
for n in nfiles:
    ...
    cursor = conn.cursor()
    cursor.execute("exec SP_my_Dailyfiles('n')")
    conn.commit()