【问题标题】:Pypyodbc: Executing stored procedure in a loop where the store procedure is saved in a tablePypyodbc:在存储过程保存在表中的循环中执行存储过程
【发布时间】:2017-12-29 21:51:07
【问题描述】:

我有一个表格,其中的数据如下所示

Name  Query   RunDate
SP    Some_sp 12/25/2017
Sp1   Some1_sp 12/25/2017
sp_2  Some2_sp 12/25/2107

查询列有要执行的存储过程。

def __init__(self):
    self.conn=pypyodbc.connect(connection)
    self.cursor=self.conn.cursor()
def getDatafromDB(self):
    sql = """Select * from table"""
    self.cursor.execute(sql)
    data=pd.DataFrame(self.cursor.fetchall())
    return data

我正在查询该表并将其保存为 pandas dateframe 。下一步是让我一个接一个地执行查询列中存在的那些存储过程。有没有办法使用 pypyodbc 做到这一点?

【问题讨论】:

    标签: python sql pandas pypyodbc


    【解决方案1】:

    试试这个:

    def getDatafromDB(self):
        sql = """Select Query from table"""
        self.cursor.execute(sql)
        data=self.cursor.fetchall()
        while data:
             print(data)
             if self.cursor.nextset():
                 sql = "{call "+data+"(?)}"
                 params = (3,)
                 cursor = connection.cursor()
                 rows = cursor.execute(sql, params).fetchall()
                 print(rows)
             else:
                 data= None
    

    【讨论】:

      猜你喜欢
      • 2010-09-15
      • 1970-01-01
      • 2016-12-03
      • 1970-01-01
      • 1970-01-01
      • 2010-09-15
      • 1970-01-01
      • 2018-01-24
      • 2013-03-04
      相关资源
      最近更新 更多