0

When I'm calling .to_sql() on a Pandas series, I get an ImportError exception:

Exception has occurred: ImportError
DLL load failed: The specified module could not be found.

I imported pandas and pyodbc, and I am able to connect to the DB server and read data through .read_sql_query() as well as use pandas Dataframes elsewhere in the code. It only shows up when I try to call .to_sql()

Pandas version 0.23.4

import pandas as pd
import numpy as np
import pyodbc
conn = pyodbc.connect('Driver={SQL Server};Server=' + prm.DATASOURCE + ';DATABASE=' + prm.DATABASE + ';UID=' + prm.USER + ';PWD=' + prm.PASSWORD)
df.to_sql('table_name', conn, if_exists='append', index=True, index_label=['country_id','year_id'])

Thank you

MarianD
  • 13,096
  • 12
  • 42
  • 54
nunoapedro
  • 13
  • 4

1 Answers1

1

From looking at this SO post and the pandas docs it seems like the problem is that DataFrame.to_sql is meant to be used with SQLAlchemy. Do you have SQLAlchemy installed? If so, maybe try passing in a SQLAlchemy engine instead of your pyodbc connection.

from sqlalchemy import create_engine

# Create your engine.
engine = create_engine('mssql+pyodbc://user:password@server/database')

# Use the engine instead of the pyodbc connection
df.to_sql('table_name', engine, if_exists='append', index=True, index_label=['country_id','year_id'])

From the Pandas docs linked above:

The pandas.io.sql module provides a collection of query wrappers to both facilitate data retrieval and to reduce dependency on DB-specific API...... If SQLAlchemy is not installed, a fallback is only provided for sqlite.

From the pandas.DataFrame.to_sql docs

con : sqlalchemy.engine.Engine or sqlite3.Connection

Using SQLAlchemy makes it possible to use any DB supported by that library. Legacy support is provided for sqlite3.Connection objects.

pistolpete
  • 968
  • 10
  • 20
  • Changed to sqlalchemy by .create_engine and that solved the problem! Didn't know you could read but not write with pyodbc directly. Thank you – nunoapedro Mar 08 '19 at 15:48
  • I didn't know that either until I looked into it, and honestly it's not intuitive at all. I wish they made it more explicit by renaming the "con" parameter to "engine". You're welcome. – pistolpete Mar 08 '19 at 15:49