本文介紹了使用SQL腳本將數(shù)據(jù)從pyodbc移動到PANAS的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習吧!
問題描述
我的問題是最初解決的問題的延伸。我對這里回答的問題沒有異議:
Move data from pyodbc to pandas
我做的不同之處:
我有一個非常長的查詢,它可以工作,但在我的Jupyter筆記本中看起來不是很好。因此,我嘗試使用OPEN(‘query.sql’)來讀入文件,而不是將其輸入。
sql = open(r'H:Common_All...query.sql').read()
df = pd.read_sql(sql,cxnn)
然后返回…
ProgrammingError Traceback (most recent call last)
~AppDataLocalContinuumanaconda3libsite-packagespandasiosql.py in
execute(self, *args, **kwargs)
1403 else:
-> 1404 cur.execute(*args)
1405 return cur
ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'GO'. (102) (SQLExecDirectW)")
During handling of the above exception, another exception occurred:
DatabaseError Traceback (most recent call last)
<ipython-input-12-f1680f29c12d> in <module>()
158
159 # Creating the dataframe from database
--> 160 df = pd.read_sql(sql, conn)
~AppDataLocalContinuumanaconda3libsite-packagespandasiosql.py in read_sql(sql, con, index_col, coerce_float, params, parse_dates, columns, chunksize)
398 sql, index_col=index_col, params=params,
399 coerce_float=coerce_float, parse_dates=parse_dates,
--> 400 chunksize=chunksize)
401
402 try:
~AppDataLocalContinuumanaconda3libsite-packagespandasiosql.py in read_query(self, sql, index_col, coerce_float, params, parse_dates, chunksize)
1437
1438 args = _convert_params(sql, params)
-> 1439 cursor = self.execute(*args)
1440 columns = [col_desc[0] for col_desc in cursor.description]
1441
~AppDataLocalContinuumanaconda3libsite-packagespandasiosql.py in execute(self, *args, **kwargs)
1414 ex = DatabaseError(
1415 "Execution failed on sql '%s': %s" % (args[0], exc))
-> 1416 raise_with_traceback(ex)
1417
1418 @staticmethod
~AppDataLocalContinuumanaconda3libsite-packagespandascompat\__init__.py in raise_with_traceback(exc, traceback)
342 if traceback == Ellipsis:
343 _, _, traceback = sys.exc_info()
--> 344 raise exc.with_traceback(traceback)
345 else:
346 # this version of raise is a syntax error in Python 3
~AppDataLocalContinuumanaconda3libsite-packagespandasiosql.py in execute(self, *args, **kwargs)
1402 cur.execute(*args, **kwargs)
1403 else:
-> 1404 cur.execute(*args)
1405 return cur
1406 except Exception as exc:
DatabaseError: Execution failed on sql...[insert the query in my sql file]
推薦答案
“Go”附近的語法不正確。
GO
不是T-SQL語句。該命令僅適用于以下SQL Server外殼:sqlcmd
、SQL Server Management Studio(SSMS)等。若要通過ODBC執(zhí)行SQL語句,您需要從文本文件中刪除GO
命令。
有關(guān)詳細信息,請參閱
SQL Server Utilities Statements – GO
這篇關(guān)于使用SQL腳本將數(shù)據(jù)從pyodbc移動到PANAS的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,