As i was following below tutorial :How To Build Web Applications with Flask Not found any good suggestion on below query.
Want to connect and execute below SQL statement on MS SQL Server (microsoft) from Flask web application.
SQL QUERY 1:
"""
USE [TESTDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [INSIM\MY14AT].[TestTable](
[Id] [int] IDENTITY(1,1) NOT NULL,
[col1] [varchar](10) NULL,
[col2] [varchar](10) NULL
) ON [PRIMARY]
GO
"""
SQL QUERY 2:
'
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Arun
-- Create date: 2024-04-27
-- Description: Test SP
-- =============================================
ALTER PROCEDURE dbo.sp_TestSP
@p1 VARCHAR(10)
,@p2 VARCHAR(10)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT * From TestTable where col1=@p1 and col2=@p2
SELECT * From TestTable where col1=@p1
END
GO
'
SQL QUERY 3 : Exec sp_TestSP @p1,@p2
With Thanks, Arun. patro.arun@gmail.com
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Heya @adorableceruleaneel,
To connect to a Microsoft SQL Server database from a Flask web application and execute SQL queries, including those for creating tables, altering procedures, and running stored procedures, you’ll need to use the appropriate Python libraries and handle database connections securely. Here’s a guide to set this up using
pyodbc
, a popular Python SQL Server database interface:Step 1: Install Necessary Packages
First, you need to install Flask and pyodbc. If you haven’t already installed these, you can do so using pip:
Step 2: Configure Database Connection
To connect to your SQL Server, you’ll need to configure the connection string. Ensure that you have the necessary drivers installed on your system (e.g., ODBC Driver for SQL Server).
Here is a sample Flask application that sets up a connection to SQL Server:
Step 3: Adjust Queries and Use
GO
Statements: Python doesn’t understand SQL’sGO
command. It’s a batch separator used in SQL Server Management Studio, not part of the SQL language. Separate commands should be executed separately if needed.Step 4: Running Your Flask Application
Run your Flask app by executing the Python script. Make sure to replace placeholders in the connection string with actual values.
You can then access endpoints defined in your Flask app to perform SQL operations. This setup provides a basic framework, and you might need to expand or adjust it based on your specific requirements, such as handling complex transactions or optimizing for performance.