在試圖寫一個pandas
「據幀到sql-server
,我得到這個錯誤:to_sql大熊貓數據幀到SQL服務器錯誤:DatabaseError
DatabaseError: Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': ('42S02', "[42S02] [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid object name 'sqlite_master'. (208) (SQLExecDirectW); [42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Statement(s) could not be prepared. (8180)")
看來pandas
正在考慮sqlite
,而不是真實的數據庫。
它不是一個連接問題,因爲我可以從sql-server
使用pandas.read_sql
連接相同的連接讀取已使用
這不是一個數據庫的權限問題,或者因爲我可以通過線寫線設置使用相同的連接參數:
cursor = conn.cursor()
cursor.execute('insert into test values (1, 'test', 10)')
conn.commit()
我可以只寫一個循環由線到instert線,但我想知道爲什麼to_sql
是不是w ^爲我而言,我擔心它不會那麼高效。
環境: Python
:2.7 Pandas
:0.20.1 sqlalchemy
:1.1.12
在此先感謝。
可運行例如:
import pandas as pd
from sqlalchemy import create_engine
import urllib
params = urllib.quote_plus("DRIVER={SQL Server Native Client 11.0};SERVER=
<servername>;DATABASE=<databasename>;UID=<username>;PWD=<password>")
engine = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
test = pd.DataFrame({'col1':1, 'col2':'test', 'col3':10}, index=[0])
test.to_sql("dbo.test", con=engine, if_exists="append", index=False)
你爲什麼想查詢一個叫表'使用SQL Server sqlite_master'?無論你,我,還是我們兩個都在這裏失去了一些東西。 –
你是怎麼調用'to_sql'的?你是否將SQLAlchemy引擎作爲[第二個參數](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_sql.html)傳遞? – unutbu
嘿!不,我不是......我只是運行'df.to_sql(「dbo.test」,con = conn,if_exists =「append」)',熊貓自動搜索sqlite_master;我不知道爲什麼!哪裏'conn = engine.connect()。connection' – AlexSB