user1700890
user1700890

Reputation: 7742

Writing pandas dataframe into SQL server table - no result and no error

Here is my code

from sqlalchemy import create_engine
import pandas as pd

engine = create_engine("connection string")
conn_obj = engine.connect()

my_df = pd.DataFrame({'col1': ['29199'], 'date_created': ['2022-06-29 17:15:49.776867']})
my_df.to_sql('SomeSQLTable', conn_obj, if_exists='append', index = False)

I also created SomeSQLTable with script:

CREATE TABLE SomeSQLTable(
col1 nvarchar(90),
date_created datetime2)
GO

Everything runs fine, but no records are inserted into SQL table and no errors are displayed. I am not sure how to troubleshoot. conn_obj works fine, I was able to pull data.

Upvotes: 1

Views: 573

Answers (1)

Muhammad Ahsan
Muhammad Ahsan

Reputation: 129

I don't think it's exactly the answer but I don't have the privileges of commenting right now.

First of all, the pd.to_sql() returns the number of rows affected by the operation, can you please check that?

Lastly, you are defining the data types in the table creation, it could be a problem of casting the data types. I never create the table through sql as pd.to_sql() can create it if needed. Thirdly, Please check on the table name, there could be an issue with the pascal case in some db's.

Upvotes: 1

Related Questions