2017-08-12 48 views
1

我試圖使用dataframe.to_SQL將pandas數據框CAPE插入到SQL Server數據庫中。我已經提到以下解決方案來插入行。 PyOdbc fails to connect to a sql server instance嘗試使用to_sql函數插入時,數據庫引擎無法連接到sql-server實例

但是我收到如下所示的錯誤。

的源代碼:

CAPE # Input dataframe 
    connection = pdc.connect('Driver={SQL Server};''Server=GIRSQL.GIRCAPITAL.com;''Database=Tableau;''uid=SQL_User;pwd=Greentableau!') 
    connection_string = urllib.parse.quote_plus(connection) 
    connection_string = "mssql+pyodbc:///?odbc_connect=%s" % connection_string 
    engine = sq.create_engine(connection_string) 
    CAPE.to_sql(engine, name='[Tableau].[dbo].[Company_Table]',if_exists='replace') 

這是我收到的錯誤:

Traceback (most recent call last): 
    File "<input>", line 1, in <module> 
    File "C:\Users\Abhay\Python36-32\lib\urllib\parse.py", line 803, in quote_plus 
string = quote(string, safe + space, encoding, errors) 
    File "C:\Users\Abhay\Python36-32\lib\urllib\parse.py", line 787, in quote 
return quote_from_bytes(string, safe) 
    File "C:\Users\Abhay\Python36-32\lib\urllib\parse.py", line 812, in quote_from_bytes 
    raise TypeError("quote_from_bytes() expected bytes") 
    TypeError: quote_from_bytes() expected bytes 
    connection = pdc.connect('Driver={SQL Server};''Server=GIRSQL.GIRCAPITAL.com;''Database=Tableau;''uid=SQL_User;pwd=Greentableau!') 
    connection_string = ur.quote(connection) 
    Traceback (most recent call last): 
    File "<input>", line 1, in <module> 
    File "C:\Users\Abhay\Python36-32\lib\urllib\parse.py", line 787, in quote 
return quote_from_bytes(string, safe) 
    File "C:\Users\Abhay\Python36-32\lib\urllib\parse.py", line 812, in 
quote_from_bytes 
    raise TypeError("quote_from_bytes() expected bytes") 
    TypeError: quote_from_bytes() expected bytes 

樣品數據幀值:

 Date Company Value  Category BICS_LEVEL_1_SECTOR_NAME BICS_LEVEL_2_INDUSTRY_GROUP_NAME BICS_LEVEL_3_INDUSTRY_NAME BICS_LEVEL_4_SUB_INDUSTRY_NAME BICS_LEVEL_5_SEGMENT_NAME BICS_REVENUE_LEVEL_ASSIGNED BS_TOT_VAL_OF_SHARES_REPURCHASED COUNTRY COUNTRY_OF_LARGEST_REVENUE EQY_SH_OUT GICS_INDUSTRY_GROUP_NAME  GICS_INDUSTRY_NAME GICS_SECTOR_NAME GICS_SUB_INDUSTRY_NAME  ICB_SECTOR_NAME   INDUSTRY_GROUP INDUSTRY_SECTOR INDUSTRY_SECTOR_NUM  INDUSTRY_SUBGROUP MARKET_SECTOR_DES Real_Earnings Real_Price CAPE_10 Percentile_10_CAPE 
     0 1975-04-30 3M Co  0   EPS    Materials      Chemicals  Specialty Chemicals   Adhesives & Sealants      NaN      10399       3635.82  US    United States 596.767   Capital Goods Industrial Conglomerates  Industrials Industrial Conglomerates General Industrials Miscellaneous Manufactur  Industrial    10011 Diversified Manufact Op   Equity    0   0  NaN     NaN 
     1 1975-04-30 3M Co  0 Stock Price    Materials      Chemicals  Specialty Chemicals   Adhesives & Sealants      NaN      10399       3635.82  US    United States 596.767   Capital Goods Industrial Conglomerates  Industrials Industrial Conglomerates General Industrials Miscellaneous Manufactur  Industrial    10011 Diversified Manufact Op   Equity    0   0  NaN     NaN 
     2 1975-04-30 3M Co  0 Cash Flow    Materials      Chemicals  Specialty Chemicals   Adhesives & Sealants      NaN      10399       3635.82  US    United States 596.767   Capital Goods Industrial Conglomerates  Industrials Industrial Conglomerates General Industrials Miscellaneous Manufactur  Industrial    10011 Diversified Manufact Op   Equity    0   0  NaN     NaN 

回答

2

DataFrame.to_sql()第一個位置參數是一個表名,你通過了engine(SQLAlchemy對象)作爲第一個參數。

那麼試試這個來代替:

CAPE.to_sql('[Tableau].[dbo].[Company_Table]',con=engine, if_exists='replace') 
+0

我在 'urllib.parse.quote_plus()' 行收到錯誤,呼籲to_sql之前。有沒有更好的方法來將數據幀插入Sql-server Db –

+0

我已將我的代碼更改爲以下 'params = urllib.parse.quote_plus(「DRIVER = {SQL Server}; SERVER = GIRSQL.GIRCAPITAL.com; DATABASE = Tableau; UID = SQL_User; PWD = Greentableau!「) engine = sqlalchemy.create_engine(」mssql + pyodbc:///?odbc_connect =%s「%params) engine.connect() df.to_sql(name =' [Tableau]。[dbo]。[Test table]',con = engine,index = False,if_exists ='append')' 現在工作正常 –

相關問題