Flask with MS Sql Server or Azure SQL

I have been searching quite a while on how to connect to MS SQL Server or Azure SQL in Flask

Its quite simple if you know how to do it …

You might want to add pyodbc to your requirements or install it with pip

import urllib

conDEBUG = “DRIVER={SQL Server};Database=TestDB;SERVER=localhost\SQLEXPRESS;UID=sa;PWD=mypass”
conDEBUG = urllib.parse.quote_plus(conDEBUG)
conDEBUG = “mssql+pyodbc:///?odbc_connect=%s” % conDEBUG

app.config[‘SQLALCHEMY_DATABASE_URI’] = conDEBUG

and after you done this

 

db = SQLAlchemy(app)

That’s it, you have a working SQL connection

And for Azure SQL just replace the first line with your Azure connection string and do not forget to modify the password