我正在使用adodbapi
python 库来查询 SSAS Cube。本质上,我的目标是要求用户首先输入帐户 ID。然后,我希望在查询的 WHERE 部分中将其作为参数 动态传递,并根据我在下面链接的文档中提到的方式在代码部分中?
包含一个参数。是我得到错误的地方,我还复制并粘贴了下面的大部分错误。cur.execute()
cur.execute()
我已将问题缩小到参数不起作用的事实。动态参数对我很有效PyODBC
,pd.read_sql
但它们似乎不适用于adodbapi
,我必须使用它,因为它是我在 python 中成功查询 SSAS 多维数据集的唯一库。
import adodbapi
import numpy as np
import pandas
account_id_input = input("Select Account ID: ").strip()
def get_df(data):
ar = np.array(data.ado_results) # turn ado results into a numpy array
df = pd.DataFrame(ar).transpose() # create a dataframe from the array
return df
with adodbapi.connect('''Provider=MSOLAP;
Data Source=MyServerName;
Initial Catalog=MySSASModel;
User ID=MyUserLogin;
Password=MyPassword;
Persist Security Info=True;
Impersonation Level=Impersonate''', 7200) as con:
with con.cursor() as cur:
sql_str = r'''SELECT
NON EMPTY
{
[Measures].[Revenue]
}
ON COLUMNS,
NON EMPTY
[AccountTable].[Year-Month].[Year-Month]
ON ROWS
FROM [Model]
WHERE([AccountTable].[Account Number].[?])
'''
args = [account_id_input]
cur.execute(sql_str, args)
data = cur.fetchall()
df = get_df(data)
df.columns = ['Year-Month','Revenue']
df
---------------------------------------------------------------------------
com_error Traceback (most recent call last)
~\.conda\envs\ProgramData\lib\site-packages\adodbapi\adodbapi.py in _execute_command(self)
681 else: #pywin32
--> 682 recordset, count = self.cmd.Execute()
683 # ----- ------------------------------- ---
~\.conda\envs\ProgramData\lib\site-packages\win32com\client\dynamic.py in Execute(self, RecordsAffected, Parameters, Options)
~\.conda\envs\ProgramData\lib\site-packages\win32com\client\dynamic.py in _ApplyTypes_(self, dispid, wFlags, retType, argTypes, user, resultCLSID, *args)
286 def _ApplyTypes_(self, dispid, wFlags, retType, argTypes, user, resultCLSID, *args):
--> 287 result = self._oleobj_.InvokeTypes(*(dispid, LCID, wFlags, retType, argTypes) + args)
288 return self._get_good_object_(result, user, resultCLSID)
com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft OLE DB Provider for Analysis Services.', 'The following system error occurred: The parameter is incorrect. ', None, 0, -2147467259), None)
During handling of the above exception, another exception occurred:
DatabaseError Traceback (most recent call last)
<ipython-input-51-87576a978d18> in <module>
35 '''
36 args = [account_id_input]
---> 37 cur.execute(sql_str, args)
38 # data = cur.fetchall()
39 # df = get_df(data)
~\.conda\envs\ProgramData\lib\site-packages\adodbapi\adodbapi.py in execute(self, operation, parameters)
873 if verbose > 3:
874 print('Params=', format_parameters(self.cmd.Parameters, True))
--> 875 self._execute_command()
876
877 def executemany(self, operation, seq_of_parameters):
~\.conda\envs\ProgramData\lib\site-packages\adodbapi\adodbapi.py in _execute_command(self)
688 format_parameters(self.cmd.Parameters, True))
689 klass = self.connection._suggest_error_class()
--> 690 self._raiseCursorError(klass, _message)
691 try:
692 self.rowcount = recordset.RecordCount
~\.conda\envs\ProgramData\lib\site-packages\adodbapi\adodbapi.py in _raiseCursorError(self, errorclass, errorvalue)
561 if eh is None:
562 eh = api.standardErrorHandler
--> 563 eh(self.connection, self, errorclass, errorvalue)
564
565 def build_column_info(self, recordset):
~\.conda\envs\ProgramData\lib\site-packages\adodbapi\apibase.py in standardErrorHandler(connection, cursor, errorclass, errorvalue)
55 cursor.messages.append(err)
56 except: pass
---> 57 raise errorclass(errorvalue)
58
59 # Note: _BaseException is defined differently between Python 2.x and 3.x
DatabaseError: (-2147352567, 'Exception occurred.', (0, 'Microsoft OLE DB Provider for Analysis Services.', 'The following system error occurred: The parameter is incorrect. ', None, 0, -2147467259), None)
Command:
我已经尝试了一切并且没有想法。我正在参考这个文档:http ://adodbapi.sourceforge.net/quick_reference.pdf和其他一些类似的stackoverflow帖子:在此处输入链接描述,但我无法弄清楚!我的 MDX 很糟糕,如果其他人对我如何重写 MDX 查询有想法,以便它在动态参数方面与 adodbapi 配合得更好,如果它实际上是库的问题。