How to save the results of select statement into Azure Blob Storage after connecting to SQL Server on Ubuntu through pyodbc using python is shown in this blog.
▼1. Saving data in SQL Server on Ubuntu into Azure Blob Storage
Based on the previous blog using Java as below, pyhon code is shown to save the results of select statement into Azure Blob Storage.
Java – Save the results of select query on SQL Server into Azure Blob Storage No.75
▼2. Prerequisites
How to build an environment using Python is shown below.
Python – How to connect to SQL Server on Ubuntu No.92
▼3. Developing Python code to save the results of select query in SQL Server into Azure Blob Storage
3-1. Creating Python code
To access Azure Blob Storage, connection_string for Azure Blob Storage contains Account key. at first, results.txt is created to save the results of select query into a file and then this file is saved into Azure Blob Storage. at last, the list of files in Azure Blob Storage is shown.
import time
import pyodbc
import random
import datetime
server = 'localhost'
database = 'testdb'
username = 'sa'
password = 'yourpassword'
driver= '{ODBC Driver 17 for SQL Server}'
success = False
retryc = 0
max_retries = 3
connection_string = 'DefaultEndpointsProtocol=https;AccountName=xxxx;AccountKey=xxxxcore.windows.net'
local_filename = '/home/xxxx/results.txt'
container_name = "files"
blob_name = "results.txt"
from azure.storage.blob import BlobServiceClient
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
# create a container and a blob client
container_client = blob_service_client.get_container_client(container_name)
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
while not success and retryc < max_retries:
try:
# create random number
num = random.randint(100, 10000000)
cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+',1433;DATABASE='+database+';UID='+username+';PWD='+ password+';',autocommit=False,Timeout=1,Trusted_Connection='no',Encrypt='no')
cursor0 = cnxn.cursor()
cursor0.execute("insert into sampledb.dbo.sampletb values ("+ str(num) + ","+ str(num) + ")")
cursor0.commit()
cursor = cnxn.cursor()
cursor.execute("SELECT * FROM sampledb.dbo.sampletb")
row = cursor.fetchone()
# save the results each line to a file
with open(local_filename, 'w') as f:
while row:
f.write(str(row[0]) + " " + str(row[1]) + "\n")
row = cursor.fetchone()
success = True
except pyodbc.Error as ex:
retryc += 1
print("Current Timestamps:" + str(datetime.datetime.now()) + "retry count: " + str(retryc))
print("==========================")
print("Error: " + str(ex))
print("==========================")
time.sleep(3)
finally:
cursor.close()
# upload the file to blob storage
with open(local_filename, "rb") as data:
blob_client.upload_blob(data)
# list the blobs in the container
blob_list = container_client.list_blobs()
for blob in blob_list:
print("\t" + blob.name)
3-2. Results of this code
python3 ./saveblobstorage.py
results.txt
▼4. Reference
- Java – Save the results of select query on SQL Server into Azure Blob Storage No.75
- Python – How to connect to SQL Server on Ubuntu No.92
- Sample code azure-sdk-for-python/blob_samples_authentication.py at main · Azure/azure-sdk-for-python · GitHub
- Quickstart: Azure Blob Storage client library for Python
That’s all. Have a nice day ahead !!!