I am trying to run this code in Azure Automation Runbook but always get the same error
cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT='+port+';
DATABASE='+database+';UID='+username+';PWD='+password+';Authentication=ActiveDirectoryPassword')
Error:
pyodbc.InterfaceError: ('IM002', u'[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
If you want to use pyodbc
module to connect Azure sql database, the running environment should have Microsoft ODBC Driver for SQL Server. If it does not have the driver, you will get the error. For more details, please refer to the document . But Azure automation sandbox does not have the driver.
According to the situation, I suggest you host the script on Azure function.
For example(I host the script on Azure HTTP trigger function)
My code
async def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
server = 'jimtestsql.database.windows.net'
database = 'test'
username = 'jim@hanxia.onmicrosoft.com'
password = 'Wdsr199545#'
driver= '{ODBC Driver 17 for SQL Server}'
cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password+';Authentication=ActiveDirectoryPassword')
cursor = cnxn.cursor()
cursor.execute("SELECT Top(1) * FROM [dbo].[StarWars]")
row = cursor.fetchone()
return func.HttpResponse(str(row[0]) + " " + str(row[1]))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.