-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLodaBlobInotMSSQL.py
36 lines (25 loc) · 1.36 KB
/
LodaBlobInotMSSQL.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#### Folllowing code demonstrate to load data from Zure blob storage to SQL database
#### Code components - create_engine for mssql+pyodbc driver and connection credential
#### Using engine.connect() method, blob storage account name, blob storage account key
#### blob storage access key, container name, and blob name, target table
#### It is using BULK insert command to insert data from CSV file to MS SQL server
#### Using connection.execute to execute bulk insert into the MS SQL server.
from sqlalchemy import create_engine
# Create an Azure SQL Database engine
engine = create_engine('mssql+pyodbc://username:password@server_name/database_name?driver=ODBC+Driver+17+for+SQL+Server')
# Establish a connection
connection = engine.connect()
# Specify the Azure Blob Storage details
blob_storage_account_name = 'storage_account_name'
blob_storage_account_key = 'storage_account_key'
container_name = 'container_name'
blob_name = 'blob_name'
# Specify the target table in Azure SQL Database
table_name = 'target_table'
# Generate the BULK INSERT command
bulk_insert_command = f"BULK INSERT {table_name} FROM 'https://{blob_storage_account_name}.blob.core.windows.net/{container_name}/{blob_name}' " \
f"WITH (FORMAT = 'CSV', FIRSTROW = 2);"
# Execute the BULK INSERT command
connection.execute(bulk_insert_command)
# Close the connection
connection.close()