I have a storage account with a container named abc360. There is a folder in this container named sqlite_db_file. Sqlite db files will be dropped in this folder. File name would like this:
ABC100102_2_20230202.db
Path to the file looks like:
abc360/sqlite_db_file/ABC100102_2_20230202.db
Storage account and container details:
Storage account: abc360stg
Container: abc360
I have an azure function app in python that is supposed to get triggered when file is dropped and copy data from file to a csv file and upload it back to same path.
So I created a function with a blob trigger, function.json looks like this:
{
"scriptFile": "__init__.py",
"bindings": [
{
"name": "myblob",
"type": "blobTrigger",
"direction": "in",
"path": "abc360/sqlite_db_file/{name}.db",
"connection": "abc360stg_STORAGE"
}
]
}
For now I am just trying to fetch all the tables that are in db file and below is my code:
import logging
import sqlite3
import os
import csv
from azure.functions import InputStream
class DataMigrator:
def __init__(self, fileName, connection_string):
self.fileName = fileName
self.connection_string = connection_string
def connect_sqlite(self):
return sqlite3.connect(self.fileName)
def get_table_names(self, cursor_sqlite):
logging.info(cursor_sqlite)
cursor_sqlite.execute("SELECT name FROM sqlite_master;")
return cursor_sqlite.fetchall()
def main(myblob: InputStream):
try:
blob_name = myblob.name.split("/")[-1]
logging.info(blob_name)
migrator = DataMigrator(blob_name, connection_string)
conn_sqlite = migrator.connect_sqlite()
logging.info("Connected to SQLite database successfully")
cursor_sqlite = conn_sqlite.cursor()
tables = migrator.get_table_names(cursor_sqlite)
logging.info(f"Tables in SQLite file: {tables}")
except Exception as e:
logging.error(f"Error: {str(e)}")
finally:
# Close SQLite connection
if conn_sqlite:
conn_sqlite.close()
Code is working fine, it connects to sqlite db file fine, but it returns an emprt array for list of tables. When I connect locally with a folder in my local drive, it works fine and lists everything.
Output (when db file is in storage account):
Tables in SQLite file: []
Following code is used because myblob.name returns abc360/sqlite_db_file/ABC100102_2_20230303.db and all i want is the filename:
myblob.name.split("/")[-1]
I am wondering is there something else thats needed here for me to read db file that is residing in storage account?
Help would be really appreciated.
I tried the code below to obtain the list of tables from the .db file in an Azure storage blob.
Code :
local.settings.json :
function.json :
Output :
The following blob trigger function code ran successfully, providing a list of tables in the .db file from the container.