0

I am thinking about using Excel as a Front-End to an Azure SQL Database.

While retrieving data from the database to Excel is fairly easy, I am struggeling to find the best way to write data from Excel back to the database.

I have found an Excel Add-In called "SQL Spreads" that seems to help with writing back from Excel to the database but it is quiet expensive.

Is it possible to do the "writing back" with VBA or Python (e.g. via xlwings and SQLAlchemy)?

Is there anybody out there who has already done something like this and can share his/her experience?

Thanks a lot in advance! :)

4
  • You can definitely use VBA to write to Azure SQL (or any database). The hard part is the authentication. There are many online examples that you could try. I repeat my comment from below: why on earth would you use Python when you can do it in VBA (which for example means you can add a nice form or do the data update via a button) Commented Aug 18, 2023 at 8:59
  • Dear Nick, thanks a lot for your feedback. I was thinking about Python simply because I have no experience in VBA. But I understand that VBA might be a better choice because VBA is better "integrated" into Excel. Can you provide me with further information/links to the respective online examples? Commented Aug 21, 2023 at 7:34
  • This was the third link when I googled it. stackoverflow.com/questions/39293669/… The code in the question should get you some way towards a solution. Once you've tried it and have a specific issue you can ask a question. I do suggest that you take the time to learn the VBA approach because there are so many Python programmers with zero experience with other languages. Commented Aug 21, 2023 at 7:53
  • I guess you could use Python if you really wanted techcommunity.microsoft.com/t5/microsoft-365-blog/… Commented Aug 23, 2023 at 5:19

1 Answer 1

0

You can use below process to write data from excel file to SQL database using python: Step1: Connect Azure SQL database using below code :

server = '<serverName>.database.windows.net'
database = '<databaseName>'
user = '<userName>'
password = '<Password>'
driver = 'ODBC Driver 17 for SQL Server'

conn_str = (
    f"DRIVER={{{driver}}};"
    f"SERVER={server};"
    f"DATABASE={database};"
    f"UID={user};"
    f"PWD={password}"
)
conn_str_encoded = urllib.parse.quote_plus(conn_str)

try:
    engine = create_engine(f"mssql+pyodbc:///?odbc_connect={conn_str_encoded}", fast_executemany=True)
    print("Passed")
except:
    print("Failed!") 

Step2: Create a data frame by reading the excel from the path using below code:

df = pd.read_excel('<filepath>', engine='openpyxl')

Write data frame into SQL database using below code:

df.to_sql('samp', con=engine, if_exists='replace', index=False)

Complete code:

import sqlalchemy as sa
from sqlalchemy import create_engine
import urllib
import pyodbc
import pandas as pd

server = '<serverNmae>.database.windows.net'
database = '<dbName>'
user = '<userName>'
password = '<password>'
driver = 'ODBC Driver 17 for SQL Server'

conn_str = (
    f"DRIVER={{{driver}}};"
    f"SERVER={server};"
    f"DATABASE={database};"
    f"UID={user};"
    f"PWD={password}"
)
conn_str_encoded = urllib.parse.quote_plus(conn_str)
try:
    engine = create_engine(f"mssql+pyodbc:///?odbc_connect={conn_str_encoded}", fast_executemany=True)
    print("Passed")
except:
    print("Failed!")

df = pd.read_excel('<excelFilePath>', engine='openpyxl')

df.to_sql('samp', con=engine, if_exists='replace', index=False)
print("Saved in the table")
print(df)

You can get the output as mentioned below after executing above code:

enter image description here

Output from SQL database:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Why use python when you can use VBA and all of the associated excel integrations?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.