{"id":4049,"date":"2024-07-22T08:38:14","date_gmt":"2024-07-22T01:38:14","guid":{"rendered":"https:\/\/www.sqlservertutorial.net\/?page_id=4049"},"modified":"2024-07-24T19:38:08","modified_gmt":"2024-07-24T12:38:08","slug":"python-sql-server","status":"publish","type":"page","link":"https:\/\/www.sqlservertutorial.net\/python-sql-server\/","title":{"rendered":"Python SQL Server"},"content":{"rendered":"\n<p>This tutorial series shows you how to work with the SQL Server from Python using the&nbsp;<a href=\"https:\/\/pypi.org\/project\/pymssql\/\" target=\"_blank\" rel=\"noreferrer noopener\">pymssql package<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='python-drivers-for-sql-server'>Python Drivers for SQL Server <a href=\"#python-drivers-for-sql-server\" class=\"anchor\" id=\"python-drivers-for-sql-server\" title=\"Anchor for Python Drivers for SQL Server\">#<\/a><\/h2>\n\n\n\n<p>To connect to the SQL Server from Python, you need a driver. There are some Python SQL Server drivers available.<\/p>\n\n\n\n<p>Here are the drivers recommended by Microsoft:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/pypi.org\/project\/pyodbc\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">pyodbc <\/a>&#8211; provides an interface for connecting to SQL Server databases via ODBC interface, allowing you to execute SQL queries seamlessly.<\/li>\n\n\n\n<li><a href=\"https:\/\/pypi.org\/project\/pyodbc\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">pymssql<\/a>  &#8211; is a Python library designed for connecting to SQL Server databases and managing data efficiently.<\/li>\n<\/ul>\n\n\n\n<p>We&#8217;ll use the <code>pymssql<\/code> package to connect to the SQL Server from Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='the-pymssql-package-features'>The pymssql Package Features <a href=\"#the-pymssql-package-features\" class=\"anchor\" id=\"the-pymssql-package-features\" title=\"Anchor for The pymssql Package Features\">#<\/a><\/h2>\n\n\n\n<p>The following highlights some features of the <code>pymssql<\/code> package:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Python 3:<\/strong> Utilize the advantage of the latest features and improvements in Python 3.<\/li>\n\n\n\n<li><strong>Unicode:<\/strong> Enable handling international characters.<\/li>\n\n\n\n<li><strong>Works across popular operating systems (OS):<\/strong> Support the most popular OSes including Windows, macOS, or Linux.<\/li>\n\n\n\n<li><strong>Written in Cython for performance:<\/strong> Benefit from faster execution and optimized performance.<\/li>\n\n\n\n<li><strong>Supports stored procedures:<\/strong> Call SQL Server&#8217;s stored procedures with return values or output parameters.<\/li>\n\n\n\n<li><strong>Supports bulk copy:<\/strong>  Quickly and efficiently transfer large amounts of data to SQL Server.<\/li>\n\n\n\n<li><strong>Can be used to connect to SQL Server on Azure:<\/strong> Seamlessly integrate with Microsoft&#8217;s cloud platform for scalable and reliable database solutions.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading\" id='getting-started'>Section 1. Getting Started <a href=\"#getting-started\" class=\"anchor\" id=\"getting-started\" title=\"Anchor for Section 1. Getting Started\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.sqlservertutorial.net\/python-sql-server\/python-connect-to-sql-server\/\">Connecting to the SQL Server<\/a> &#8211; show you how to create a Python program that connects to the SQL Server.<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading\" id='inserting-data'>Section 2. Inserting Data <a href=\"#inserting-data\" class=\"anchor\" id=\"inserting-data\" title=\"Anchor for Section 2. Inserting Data\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.sqlservertutorial.net\/python-sql-server\/python-sql-server-insert\/\">Insert data into a table<\/a> &#8211; show you how to insert data into a table in SQL Server from Python<\/li>\n\n\n\n<li><a href=\"https:\/\/www.sqlservertutorial.net\/python-sql-server\/python-sql-server-import-from-csv\/\">Import a CSV file into a table<\/a> &#8211; learn how to develop a Python program that reads data from a CSV file and imports it into a table in the SQL Server.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.sqlservertutorial.net\/python-sql-server\/python-sql-server-bulk-copy\/\">Bulk Copy<\/a> &#8211; discover how to quickly load data into an SQL Server table using the bulk copy function.<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading\" id='updating-data'>Section 3. Updating Data <a href=\"#updating-data\" class=\"anchor\" id=\"updating-data\" title=\"Anchor for Section 3. Updating Data\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.sqlservertutorial.net\/python-sql-server\/python-sql-server-update\/\">Update data from a table<\/a> &#8211; Learn how to update data from a table using Python.<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading\" id='querying-data'>Section 4. Querying Data <a href=\"#querying-data\" class=\"anchor\" id=\"querying-data\" title=\"Anchor for Section 4. Querying Data\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.sqlservertutorial.net\/python-sql-server\/python-sql-server-select\/\">Select data from a table<\/a> &#8211; Guide you on how to select one or more rows from a table in Python<\/li>\n\n\n\n<li><a href=\"https:\/\/www.sqlservertutorial.net\/python-sql-server\/python-sql-server-pagination\/\">Pagination<\/a> &#8211; Show you how to break up a large result set into smaller chunks (pages) using FETCH and OFFSET clauses.<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading\" id='deleting-data'>Section 5. Deleting Data <a href=\"#deleting-data\" class=\"anchor\" id=\"deleting-data\" title=\"Anchor for Section 5. Deleting Data\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.sqlservertutorial.net\/nodejs-sql-server\/nodejs-sql-server-delete\/\">Delete data from a table<\/a> &#8211; Show you how to delete data from a table in SQL Server from a Python application.<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading\" id='handling-transactions'>Section 6. Handling Transactions <a href=\"#handling-transactions\" class=\"anchor\" id=\"handling-transactions\" title=\"Anchor for Section 6. Handling Transactions\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.sqlservertutorial.net\/python-sql-server\/python-sql-server-transaction\/\">Perform a transaction<\/a> &#8211; learn how to perform an SQL Server transaction from Python.<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading\" id='calling-stored-procedures'>Section 7. Calling Stored Procedures <a href=\"#calling-stored-procedures\" class=\"anchor\" id=\"calling-stored-procedures\" title=\"Anchor for Section 7. Calling Stored Procedures\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.sqlservertutorial.net\/python-sql-server\/python-sql-server-call-stored-procedures\/\">Call a stored procedure<\/a> &#8211; Learn to call a stored procedure in SQL Server from Python.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.sqlservertutorial.net\/python-sql-server\/python-sql-server-call-stored-procedures-with-output-parameters\/\">Call a stored procedure with OUTPUT parameters<\/a> &#8211; Show you how to call stored procedures with OUTPUT parameters from Python.<\/li>\n<\/ul>\n<\/div><\/div>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"4049\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlservertutorial.net\/python-sql-server\/\"\n\t\t\t\tdata-post-title=\"Python SQL Server\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"4049\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlservertutorial.net\/python-sql-server\/\"\n\t\t\t\tdata-post-title=\"Python SQL Server\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial series shows you how to work with the SQL Server from Python using the&nbsp;pymssql package. Python Drivers for SQL Server # To connect to the SQL Server from Python, you need a driver. There are some Python SQL Server drivers available. Here are the drivers recommended by Microsoft: We&#8217;ll use the pymssql package [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":20,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-4049","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python SQL Server<\/title>\n<meta name=\"description\" content=\"This tutorial series shows you step-by-step how to work with the SQL Server from Python using the\u00a0pymssql package.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlservertutorial.net\/python-sql-server\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python SQL Server\" \/>\n<meta property=\"og:description\" content=\"This tutorial series shows you step-by-step how to work with the SQL Server from Python using the\u00a0pymssql package.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlservertutorial.net\/python-sql-server\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Server Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2024-07-24T12:38:08+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/python-sql-server\\\/\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/python-sql-server\\\/\",\"name\":\"Python SQL Server\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/#website\"},\"datePublished\":\"2024-07-22T01:38:14+00:00\",\"dateModified\":\"2024-07-24T12:38:08+00:00\",\"description\":\"This tutorial series shows you step-by-step how to work with the SQL Server from Python using the\u00a0pymssql package.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/python-sql-server\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.sqlservertutorial.net\\\/python-sql-server\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/python-sql-server\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python SQL Server\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/#website\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/\",\"name\":\"SQL Server Tutorial\",\"description\":\"The Practical SQL Server Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python SQL Server","description":"This tutorial series shows you step-by-step how to work with the SQL Server from Python using the\u00a0pymssql package.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlservertutorial.net\/python-sql-server\/","og_locale":"en_US","og_type":"article","og_title":"Python SQL Server","og_description":"This tutorial series shows you step-by-step how to work with the SQL Server from Python using the\u00a0pymssql package.","og_url":"https:\/\/www.sqlservertutorial.net\/python-sql-server\/","og_site_name":"SQL Server Tutorial","article_modified_time":"2024-07-24T12:38:08+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlservertutorial.net\/python-sql-server\/","url":"https:\/\/www.sqlservertutorial.net\/python-sql-server\/","name":"Python SQL Server","isPartOf":{"@id":"https:\/\/www.sqlservertutorial.net\/#website"},"datePublished":"2024-07-22T01:38:14+00:00","dateModified":"2024-07-24T12:38:08+00:00","description":"This tutorial series shows you step-by-step how to work with the SQL Server from Python using the\u00a0pymssql package.","breadcrumb":{"@id":"https:\/\/www.sqlservertutorial.net\/python-sql-server\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlservertutorial.net\/python-sql-server\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlservertutorial.net\/python-sql-server\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlservertutorial.net\/"},{"@type":"ListItem","position":2,"name":"Python SQL Server"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlservertutorial.net\/#website","url":"https:\/\/www.sqlservertutorial.net\/","name":"SQL Server Tutorial","description":"The Practical SQL Server Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlservertutorial.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/4049","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/comments?post=4049"}],"version-history":[{"count":5,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/4049\/revisions"}],"predecessor-version":[{"id":4190,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/4049\/revisions\/4190"}],"wp:attachment":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/media?parent=4049"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}