I am stuck connecting to my database. I'm not sure why the error as follows keep coming out even though I've followed the documentation clearly.
sqlalchemy.exc.InterfaceError: (pyodbc.InterfaceError) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
My connection string looks like this
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
from sqlalchemy import desc, create_engine
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mssql+pyodbc://localhost\SQLEXPRESS/master?driver=ODBC Driver 17 for SQL Server?Trusted_Connection=True'
app.config['SQLALCEHMY_MODIFICATIONS'] = False
db = SQLAlchemy()
ma = Marshmallow()
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100))
description = db.Column(db.String(200))
author = db.Column(db.String(50))
def __init__(self, title, description, author):
self.title = title
self.description = description
self.author = author
class PostSchema(ma.Schema):
class Meta:
fields = ("title", "description", "author")
post_schema = PostSchema()
posts_schema = PostSchema(many=True)
@app.route('/', methods=['GET'])
def get_post():
return jsonify({"Hello": "World"})
@app.route('/post', methods=['POST'])
def add_post():
title = request.json['title']
description = request.json['description']
author = request.json['author']
my_post = Post(title, description, author)
db.session.add(my_post)
db.session.commit()
return post_schema.jsonify(my_post)
db.init_app(app)
ma.init_app(app)
if __name__ == "__main__":
app.run(debug=True)
Is there anywhere in which I left out? Whenever I tried to create a post request through postman, it will show the error as stated above. I am sure the ODBC driver exist and I've connected it to the local database that I have installed in my PC. Also, table Post is already created on my database such as follows:
USE [master]
GO
/****** Object: Table [dbo].[Post] Script Date: 23-Mar-22 1:30:52 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Post](
[id] [int] IDENTITY(1,1) NOT NULL,
[title] [nvarchar](200) NULL,
[description] [nvarchar](200) NULL,
[author] [nvarchar](200) NULL,
CONSTRAINT [PK_ID] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
Thanks for any suggestions