简体   繁体   English

Python到SQL Server存储过程

[英]Python to SQL Server Stored Procedure

I am trying to call a SQL Server stored procedure from my Python code, using sqlalchemy. 我试图使用sqlalchemy从我的Python代码调用SQL Server存储过程。 What I'm finding is that no error is raised by the python code and the stored procedure is not executing. 我发现的是python代码没有引发错误,并且存储过程没有执行。

Sample code: 示例代码:

def SaveData(self, aScrapeResult):
    sql = "EXECUTE mc.SaveFundamentalDataCSV @pSource='%s',@pCountry='%s',@pOperator='%s',@pFromCountry='%s',@pFromOperator='%s',@pToCountry='%s',@pToOperator='%s',@pSiteName='%s',@pFactor='%s',@pGranularity='%s',@pDescription='%s',@pDataType='%s',@pTechnology = '%s',@pcsvData='%s'"

    #   Need to convert the data into CSV
    util = ListToCsvUtil()
    csvValues = util.ListToCsv(aScrapeResult.DataPoints)

    formattedSQL = sql % (aScrapeResult.Source ,aScrapeResult.Country,aScrapeResult.Operator ,aScrapeResult.FromCountry ,aScrapeResult.FromOperator ,aScrapeResult.ToCountry ,aScrapeResult.ToOperator ,aScrapeResult.SiteName ,aScrapeResult.Factor ,aScrapeResult.Granularity ,aScrapeResult.Description ,aScrapeResult.DataType ,aScrapeResult.Technology ,csvValues)

    DB = create_engine(self.ConnectionString)
    DB.connect()

    result_proxy = DB.execute(formattedSQL)

    results = result_proxy.fetchall()

Examination of formatted SQL yields the following command 检查格式化的SQL会产生以下命令

EXECUTE mc.SaveFundamentalDataCSV @pSource='PythonTest', @pCountry='UK',
  @pOperator='Operator', @pFromCountry='None', @pFromOperator='None', 
  @pToCountry='None', @pToOperator='None', @pSiteName='None', @pFactor='Factor', 
  @pGranularity='Hourly', @pDescription='Testing from python', 
  @pDataType='Forecast',@pTechnology = 'Electricity',
  @pcsvData='01-Jan-2012 00:00:00,01-Feb-2012 00:15:00,1,01-Jan-2012 00:00:00,01-Feb-2012 00:30:00,2';

The various versions and software in use is as follows: SQL Server 2008 R2 Python 2.6.6 SQLAlchemy 0.6.7 使用的各种版本和软件如下:SQL Server 2008 R2 Python 2.6.6 SQLAlchemy 0.6.7

I have tested my stored procedure by calling it directly in SQL Server Management Studio with the same parameters with no problem. 我已经测试了我的存储过程,直接在SQL Server Management Studio中使用相同的参数调用它,没有任何问题。

It's worth stating that this point that the Python version and the SQL server version are non-changeable. 值得指出的是,Python版本和SQL服务器版本是不可更改的。 I have no strong allegiance to sqlalchemy and am open to other suggestions. 我对sqlalchemy没有强烈的忠诚,并对其他建议持开放态度。

Any advice would be greatly appreciated, more information can be provided if needed. 任何建议将不胜感激,如果需要可以提供更多信息。

Fixed now but open to opinion if I'm using best practice here. 现在修复但是如果我在这里使用最佳实践则愿意接受意见。 I've used the 'text' object exposed by sqlalchemy, working code below: 我使用了sqlalchemy公开的'text'对象,工作代码如下:

def SaveData(self, aScrapeResult):
    sql = "EXECUTE mc.SaveFundamentalDataCSV @pSource='%s',@pCountry='%s',@pOperator='%s',@pFromCountry='%s',@pFromOperator='%s',@pToCountry='%s',@pToOperator='%s',@pSiteName='%s',@pFactor='%s',@pGranularity='%s',@pDescription='%s',@pDataType='%s',@pTechnology = '%s',@pcsvData='%s'"

    #   Need to convert the data into CSV
    util = ListToCsvUtil()
    csvValues = util.ListToCsv(aScrapeResult.DataPoints)

    formattedSQL = sql % (aScrapeResult.Source ,aScrapeResult.Country,aScrapeResult.Operator ,aScrapeResult.FromCountry ,aScrapeResult.FromOperator ,aScrapeResult.ToCountry ,aScrapeResult.ToOperator ,aScrapeResult.SiteName ,aScrapeResult.Factor ,aScrapeResult.Granularity ,aScrapeResult.Description ,aScrapeResult.DataType ,aScrapeResult.Technology ,csvValues)

    DB = create_engine(self.ConnectionString)
    conn = DB.connect()

    t = text(formattedSQL).execution_options(autocommit=True)

    DB.execute(t)

    conn.close()

Hope this proves helpful to someone else! 希望这证明对别人有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM