0

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

6
  • looks like its trying to connect to localhost\SQLEXPRESS and database master? ... I dont think thats what you actually want ... at a minimum you need to escape the backslash I think localhost\\SQLEXPRESS Commented Mar 23, 2022 at 5:38
  • this looks relevant stackoverflow.com/a/24085353/541038 Commented Mar 23, 2022 at 5:40
  • yes that is the name of my server and database.. adding double backslash doesnt seems to solve the issue Commented Mar 23, 2022 at 5:46
  • In its default configuration SQL Server Express edition isn't listening on either TCP/IP or Named Pipes protocols. Have you gone into SQL Server (version) Configuration Manager yet and enabled the TCP/IP protocol? You'll need to restart both the SQL Server service and the SQL Browser service after making changes here so that the Instance Name resolution of \SQLEXPRESS actually works. Commented Mar 23, 2022 at 6:32
  • You also have two ? characters in your connection Uri. Commented Mar 23, 2022 at 6:33

1 Answer 1

0

As reviewed, The connection URI was missing escape of the backslash and '&' instead of '?' symbol. Thanks for the suggestions.

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

Comments

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.