0

I am new to databricks and I need to schedule some code that transforms and writes data to an azure sql database using python.

I get the following error: DBAPIError: (pyodbc.Error) ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) (SQLDriverConnect)")

Running the code locally works fine and writes the data to the azure sql db.

Herebelow a sample of my code

server = "example.database.windows.net"
database = ""
username = ""
password = ""

driver = '{ODBC Driver 17 for SQL Server}'

odbc_str = 'DRIVER='+driver+';SERVER='+server+';PORT=1433;UID='+username+';DATABASE='+ database + ';PWD='+ password
connect_str = 'mssql+pyodbc:///?odbc_connect=' + urllib.parse.quote_plus(odbc_str)
engine = create_engine(connect_str)

df = pd.DataFrame({"Def": [1,2,3,4,5]})

def to_sql(df, table):
    df.to_sql(table, engine, if_exists = "replace", index=False, chunksize = 100)

to_sql(df, "Def")
1

1 Answer 1

2

By default, Azure Databricks does not have ODBC Driver installed.

Make sure you have installed MY SQL ODBC Driver before running the above command.

Run the following commands in a single cell to install MY SQL ODBC Driver on the Azure Databricks cluster.

%sh
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get -q -y install msodbcsql17

enter image description here

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

1 Comment

One more question, I pull data from a REST API using Python in databricks and write the output to an azure sql db. Pulling the data from the api goes rapidly but writing it to azure sql goes terribly slow. Is this a recommended approach or would you advise differently?

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.