0

I have a dataframe that I want to upload to a SQL Server database.

I have been looking at the pandas to_sql method but I can't seem to get it to work.

My dataframe is say 500 rows with 3 columns

column name      data type
dateLg           datetime
temperature      float64
city             object

And so my SQL table is called tblCityTemperature

  column name          data type
  DateLeg              datetime
  Temp                 float
  CC                   nvarchar(20)

Is there a way of mapping the columns in a dataframe to my sql table?

I tried the following where tblColNames is the three names in my sql table.

df.to_sql("tblPrices", cnxn, index_label=tblColNames)

I get the error message below which doesn't really make sense to me

ValueError: Length of 'index_label' should match number of levels, which is 1

1 Answer 1

2

You can change your columns before to_sql

df.columns=['DateLeg','Temp','CC']

Then you just need

df.to_sql("tblPrices", cnxn, if_exists ='append')
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, does it matter if the columns are in a different order to the sql they are being appended to? I'm getting this error... DatabaseError: Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': ('42S02', "[42S02] [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'sqlite_master'. (208) (SQLExecDirectW); [42S02] [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared. (8180)")
I am to get data out of the sql table just fine
@mHelpMe if the name same it is ok , and you may need adding index=False as well since you append to an exist table
@mHelpMe also it is better check the way mentioned here stackoverflow.com/questions/43136121/…, change the way you are using
thanks just trying that now... still having database issues. Will keep plugging away at it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.