1

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.

1
  • 1
    After creating your engine, set engine.echo = True and then look at the log output to check for INSERT INTO [SomeSQLTable] (col1, date_created) VALUES (?, ?), followed by the parameter values, and then a COMMIT. Commented Jun 30, 2022 at 14:07

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.