Create Python apps using SQL Server
Create Python apps using SQL Server on Windows
In this section, you will get SQL Server 2017 on Windows. After that you will install the necessary dependencies to create Python apps with SQL Server.
Step 1.1 Install SQL Server
If you don’t have SQL Server 2017 Developer (or above) installed, click here to download the SQL Server exe.
Run it to start the SQL installer.
Click Basic in Select an installation type.
Click Accept after you have read the license terms.
(Optional) if you need to, you can choose a custom installation location for SQL Server.
Click Install to proceed with the installation.
You now have SQL Server installed and running locally on your Windows computer! Check out the next section to continue installing prerequisites.
Step 1.2 Install Python
Download and run the installer here
Next, add Python to your path
Press start
Search for “Advanced System Settings”
Click on the “Environment Variables” button
Add the location of the Python27 folder to the PATH variable in System Variables. The following is a typical value for the PATH variable C:\Python27
You have succesfully installed Python on your machine!
Step 1.3 Install the ODBC Driver and SQL Command Line Utility for SQL Server
SQLCMD is a command line tool that enables you to connect to SQL Server and run queries.
Install the ODBC Driver.
Install the SQL Server Command Line Utilities.
After installing SQLCMD, you can connect to SQL Server using the following command from a CMD session:Terminal
This how to run a basic inline query. The results will be printed to STDOUT.Terminal
Results
You have successfully installed SQL Server Command Line Utilities on your Windows machine!
Step 2.1 Install the Python driver for SQL Server
Terminal
Step 2.2 Create a database for your application
Connect to SQL Server using SQLCMD and execute the following statement to create a database called SampleDB.Terminal
Step 2.3 Create a Python app that connects to SQL Server and executes queries
Create a new folder for the sampleTerminal
Execute the T-SQL scripts below in the terminal with sqlcmd to a table and insert some row.Terminal
Using your favorite text editor, create a new file called crud.py in the SqlServerSample folder. Paste the code below inside into the new file. This will insert, update, delete, and read a few rows.Python
Run your Python script from the terminal.Terminal
Results
Congratulations! You created your first Python app with SQL Server! Check out the next section to learn about how you can make your Python app faster with SQL Server’s Columnstore feature.
Step 3.1 Create a new table with 5 million using sqlcmd
Terminal
Step 3.2 Create a Python app that queries this tables and measures the time taken
Terminal
Using your favorite text editor, create a new file called columnstore.py in the SqlServerColumnstoreSample folder. Paste the following code inside it.Python
Step 3.3 Measure how long it takes to run the query
Run your Python script from the terminal.Terminal
Results
Step 3.4 Add a columnstore index to your table.
Terminal
Step 3.5 Measure how long it takes to run the query with a columnstore index
Terminal
Results
Congratulations! You just made your Python app faster using Columnstore Indexes!
Reference : https://www.microsoft.com/en-us/sql-server/developer-get-started/python/windows
Last updated