0

I am trying to connect to azure sql database through Jupyter notebook and later to load the data into excel/csv .I have the details of server and database only .Username & password i think by default its taking my desktop credentials(unsure).

Here is tried code

import pyodbc 
cnxn = pyodbc.connect(Server=myserver;Database=mydatabase)
Trend
  • 1
  • 1
  • 4
  • `cnxn = pyodbc.connect('DRIVER=' + driver + ';SERVER=' + server + ';PORT=1433;DATABASE=' + database + ';UID=' + username + ';PWD=' + password)` You can give Username and Password in this format – Aswin Jan 18 '23 at 11:14
  • Have you found a solution? does my answer is acceptable or you need more help? – Lidor Eliyahu Shelef Jan 20 '23 at 17:00

2 Answers2

2

In order to connect to your Azure SQL database with your jupyter notebook use the following:

import pyodbc

server = 'tcp:SQLSERVER.database.windows.net' # Server example
database = '<INSERT DATABASE NAME>' 
username = '<INSERT USERNAME>' 
password = '<INSER PASSWORD>' 
driver= '{ODBC Driver 17 for SQL Server}' # Driver example
connection= pyodbc.connect('DRIVER=' + driver + ';SERVER=' +server + ';PORT=1433;DATABASE=' + database +';UID=' + username + ';PWD=' + password)

cursor = connection.cursor() # Just something you can do
print(connection)
connection.close()

For more details you can refer to the following links:

  1. Connect to Azure SQL Database using Python and Jupyter Notebook
  2. Connect to Azure SQL Database in a Jupyter Notebook using Python
  3. Quickstart: Use Python to query a database
Lidor Eliyahu Shelef
  • 1,299
  • 1
  • 14
  • 35
  • ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)') i am getting this error, so the ODBC installation to be done? will the driver installation be independent based on the projects? – Trend Jan 18 '23 at 12:54
  • you've run the code I specified? you've created the database according to the guides or did you do some custom configurations? (either way I reccomend checking the third link I supplied to you, it explains everything) – Lidor Eliyahu Shelef Jan 18 '23 at 13:04
  • @Trend try this following answer to your error: [Data source name not found and no default driver specified](https://stackoverflow.com/questions/46045834/pyodbc-data-source-name-not-found-and-no-default-driver-specified) – Lidor Eliyahu Shelef Jan 18 '23 at 13:25
0

You need to give username, password of the Azure SQL database in connextion. Below is the code to establish connection of Azure SQl database using python in Jupyter Notebook.

import pyodbc
# Establish the connection
server =  'xxxxx.database.windows.net'
database =  'xxx'
username =  'xxxx'
password =  'xxxx'
driver=  '{ODBC Driver 17 for SQL Server}'
conn = pyodbc.connect('DRIVER='  + driver +  ';SERVER='  +
server +  ';PORT=1433;DATABASE='  + database +
';UID='  + username +  ';PWD='  + password)
print(conn)
conn.close()

enter image description here

Reference: Use Python to query a database - Azure SQL Database & SQL Managed Instance | Microsoft Learn

Aswin
  • 4,090
  • 2
  • 4
  • 16