mwilson
mwilson

Reputation: 12900

SQLAlchemy - SQL Server connection

I have used postgresql with SQLAlchemy without issue but am trying to get at database that's in Azure running on SQL Server. I am unable to connect and can't really figure out why. I have searched quite a bit and have come across most articles pointing me towards using the create_engine but I can't seem to get that to work with using ORM. Here is my current code:

EXCEPTION

sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:sqlserver

from flask import Flask
from models import db, People

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlserver://<connectionString>'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)

@app.route('/')
def hello_world():
    #Exception here
    people = People.query.filter_by(birthYear=12).all()
    return 'Hello World!'


if __name__ == '__main__':
    app.run()

Azure offers me 4 different connection strings:

Is there any way to make SQLAlchemy work with SQL Server?

https://docs.sqlalchemy.org/en/latest/dialects/mssql.html

Upvotes: 0

Views: 3526

Answers (1)

Jacky Wang
Jacky Wang

Reputation: 3490

SQLAlchemy is just a high level ORM library, the underlying communication to db is done by the dialect/DBAPI libraries.

Assume that you are planing to use pymssql, you need to install pymssql first, and modify the connection string

mssql+pymssql://<username>:<password>@<host>:<port>/<database_name>/?charset=utf8

Modern versions of this driver work very well with SQL Server and FreeTDS from Linux and is highly recommended.

Upvotes: 1

Related Questions