Search code examples
pythonsqlazureazure-sql-databaseazure-machine-learning-service

Error in connecting Azure SQL database from Azure Machine Learning Service using python


I am trying to connect Azure SQL Database from Azure Machine Learning service, but I got the below error.

Please check Error: -

**('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found and no default driver specified (0) (SQLDriverConnect)')**

Please Check the below code that I have used for database connection: -

import pyodbc

class DbConnect:
    # This class is used for azure database connection using pyodbc
    def __init__(self):
        try:
            self.sql_db = pyodbc.connect(SERVER=<servername>;PORT=1433;DATABASE=<databasename>;UID=<username>;PWD=<password>')

            get_name_query = "select name from contacts"
            names = self.sql_db.execute(get_name_query)
            for name in names:
                print(name)

        except Exception as e:
            print("Error in azure sql server database connection : ", e)
            sys.exit()

if __name__ == "__main__":
    class_obj = DbConnect()

Is there any way to solve the above error? Please let me know if there is any way.


Solution

  • I'd consider using azureml.dataprep over pyodbc for this task (the API may change, but this worked last time I tried):

    import azureml.dataprep as dprep
    
    ds = dprep.MSSQLDataSource(server_name=<server-name,port>,
                               database_name=<database-name>,
                               user_name=<username>,
                               password=<password>)
    

    You should then be able to collect the result of an SQL query in pandas e.g. via

    dataflow = dprep.read_sql(ds, "SELECT top 100 * FROM [dbo].[MYTABLE]")
    dataflow.to_pandas_dataframe()