mismaah
mismaah

Reputation: 356

Connect Flask app to SQL Server using Flask-SQLAlchemy with Windows authentication

I followed this to setup with Windows authentication. Don't know if it works as there is an error right now.

Right now I have this:

from flask import Flask
from flask_sqlalchemy import sqlalchemy
app = Flask(__name__, static_url_path='', static_folder='static')
app.config['SQLACHLEMY_DATABASE_URI'] = 'mssql://LAPTOP-GNBBOVKT/SQLEXPRESS/OIS?trusted_connection=yes'
db = sqlalchemy(app)

class Country(db.Model):
    __tablename__ = 'example'
    id = db.Column('Country_ID', db.Integer, primary_key=True)
    code = db.Column('Country_Code', db.String)

@app.route('/test')
def test():
    allCountries = Country.query.all()
    return jsonify(allCountries)

But I'm getting the error:

TypeError: 'module' object is not callableatdb = sqlalchemy(app)

EDIT: Fixed the error by changing sqlalchemy to SQLAlchemy but now I'm getting

UserWarning: Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:///:memory:".

Probably because it's not connected to the database.

EDIT: What finally fixed it was importing pyodbc and changing URI line:

app.config['SQLALCHEMY_DATABASE_URI'] = 'mssql+pyodbc://@' + 'LAPTOP-GNBBOVKT\\SQLEXPRESS' + '/' + 'OIS' + '?trusted_connection=yes&driver=ODBC+Driver+13+for+SQL+Server'

Upvotes: 0

Views: 2120

Answers (1)

DevHyperCoder
DevHyperCoder

Reputation: 943

You should initialise the db object by db = SQLAlchemy ()

Also change your import to from flask_sqlaclhemy import SQLAlchemy

Edit: There is a typo Change to app.config['SQLALCHEMY_DATABASE_URI'] There's a typo

Upvotes: 1

Related Questions