0

I am trying to write out a sample table to Microsoft SQL Server Management Studio. I am able to write out to SSMS from python. Here is my code. I cannot include the information in the connection, but it works for reading in information from SSMS. I get the error below. Necessary imports are pandas, sqlalchemy, pyodbc,

 df2 = pd.DataFrame({'key':['a','a','b'],
                        'val':[435,7,6]})
    
cnxn = pyodbc.connection() # Have the correct connection here but can't include
cn= create_engine(cnxn)
rows_uploaded= df2.to_sql(OUTPUT_TABLE_NAME, cn, schema = 'sample_schema', if_exists = 'replace', index = False, chunksize = 1000)

AttributeError: 'pyodbc.Connection' object has no attribute '_instantiate_plugins'

Scott
  • 1
  • You can't use a `pyodbc.Connection` object with `.to_sql()`. You'll need to create a SQLAlchemy `Engine` object and pass that as the second argument to `.to_sql()`. See [this answer](https://stackoverflow.com/a/71083448/2144390) for the easy way to do that. – Gord Thompson Aug 03 '22 at 18:39
  • @GordThompson I don't understand. I pass cn into my .to_sql, not cnxn. – Scott Aug 03 '22 at 18:53
  • Passing a pyodbc.Connection object to `create_engine()` does not work (as you have discovered). Use `create_engine(URL.create(…, query={"odbc_connect": …})` as described in the [answer cited above](https://stackoverflow.com/a/71083448/2144390). – Gord Thompson Aug 05 '22 at 15:18

0 Answers0