{"id":23377,"date":"2023-01-23T07:10:00","date_gmt":"2023-01-23T06:10:00","guid":{"rendered":"https:\/\/expert-only.com\/?p=23377"},"modified":"2023-03-15T21:12:58","modified_gmt":"2023-03-15T20:12:58","slug":"python-import-csv-data-to-sql-server","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/","title":{"rendered":"How to import data from CSV to SQL Server in Python ?"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\" id=\"h-step-by-step-coding-tutorial-on-how-to-import-data-from-a-csv-file-into-a-sql-server-table-using-python\"><strong><em>Step-by-step coding tutorial on how to import data from a CSV file into a SQL Server table using Python.<\/em><\/strong><\/h4>\n\n\n\n<p>In this tutorial, we will learn how to import data from CSV files into SQL Server using only Python scripts. By the end of all the steps, you will know how to connect to SQL Server and read data from flat files using Python.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/#1-import-the-necessary-python-libraries\" >1. Import the necessary Python libraries<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/#2-define-the-path-to-the-csv-source-file-and-create-it\" >2. Define the path to the CSV source file and create it<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/#3-connect-to-the-sql-server-database\" >3. Connect to the SQL Server database<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/#4-create-the-sql-server-target-table\" >4. Create the SQL Server target table<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/#5-insert-data-from-the-csv-file-into-sql-server\" >5. Insert data from the CSV file into SQL Server<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/#wrap-up-all-the-steps-into-one-functional-script\" >Wrap up all the steps into one functional script<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/#conclusion-on-importing-a-csv-file-into-sql-server-using-python\" >Conclusion on importing a CSV file into SQL Server using Python<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1-import-the-necessary-python-libraries\"><\/span>1. Import the necessary Python libraries<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>First, we need to import the necessary libraries. We will use <em>pandas<\/em> to read the data from the CSV file and <em>pyodbc<\/em> to connect to the SQL Server database and execute SQL commands.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import pandas as pd\nimport pyodbc\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2-define-the-path-to-the-csv-source-file-and-create-it\"><\/span>2. Define the path to the CSV source file and create it<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Next, we need to define the path to the CSV file that we want to import. In this example, we will assume that the CSV file is located on the same <a href=\"https:\/\/expert-only.com\/en\/windows\/how-to-enable-dark-mode-in-windows-10\/\">Windows machine<\/a> as our Python development environment.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">csv_file = 'c:\\data\\employees.csv'\n<\/pre>\n\n\n\n<p>For example, you can use a very simple file like the one below. It was used in the previous <a href=\"https:\/\/expert-only.com\/en\/python\/export-sql-server-data-to-csv-in-python\/\">tutorial on how to export data into CSV format using Python<\/a>. The data can look like this, i.e. with 4 columns: <\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>ID<\/li>\n\n\n\n<li>Name<\/li>\n\n\n\n<li>Department<\/li>\n\n\n\n<li>Salary<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">id,name,department,salary\n1,John Smith,Sales,50000\n2,Jane Doe,Marketing,60000\n3,Bob Johnson,IT,70000\n4,Alice Wong,HR,55000\n<\/pre>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/expert-only.com\/en\/python\/export-sql-server-data-to-csv-in-python\/\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3-connect-to-the-sql-server-database\"><\/span>3. Connect to the SQL Server database<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Now, we need to connect to <a href=\"https:\/\/expert-only.com\/en\/dba\/create-sql-server-database-with-ssms\/\">the SQL Server database<\/a> using the pyodbc Python module. We will need to provide the connection details, such as the server name, database name, username, and password. Or in my case I&#8217;m using a Windows authentication, like in the script below.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># declare some variables to store the connections details\ndriver = 'SQL Server'\nserver = 'localhost'\ndatabase = 'Expert-Only'\n\n# connect to the local SQL Server database\nconnection = pyodbc.connect(f'DRIVER={driver};'\n                            f'SERVER={server};'\n                            f'DATABASE={database};'\n                            f'Trusted_Connection=yes;')\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4-create-the-sql-server-target-table\"><\/span>4. Create the SQL Server target table<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>If the SQL Server table does not exist yet, of course we need to create it. In this example, we will assume that the SQL Server table has the same structure as the CSV file, and we will create it using the following SQL command.<\/p>\n\n\n\n<p>The goal is to avoid errors by checking if the table already exists or not in the database. So we are sure to drop the table and create the proper structure without any errors. This specific code below first attempts to drop the table <code>employees<\/code> if it exists, and then it creates the table with the specified schema.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">drop_table_query = \"\"\"\nDROP TABLE IF EXISTS employees\n\"\"\"\n\ncreate_table_query = \"\"\"\nCREATE TABLE employees (\n    id INT PRIMARY KEY,\n    first_name VARCHAR(50),\n    last_name VARCHAR(50),\n    email VARCHAR(50),\n    hire_date DATE\n)\n\"\"\"\n\ncursor.execute(drop_table_query)\ncursor.execute(create_table_query)\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"5-insert-data-from-the-csv-file-into-sql-server\"><\/span>5. Insert data from the CSV file into SQL Server<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Next, we need to read the data from the CSV file using &#8220;pandas&#8221;, and then insert it into the SQL Server table using SQL commands. We will assume that the first row of the CSV file contains the column names. Then the last line of code simply commits the changes to the database. It is a best practice to always commit the code in the database and close connections.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">df = pd.read_csv(csv_file)\nfor index, row in df.iterrows():\n    insert_query = f\"\"\"\n    INSERT INTO employees (id, first_name, last_name, email, hire_date)\n    VALUES ({row['id']}, '{row['first_name']}', '{row['last_name']}', '{row['email']}', '{row['hire_date']}')\n    \"\"\"\n    cursor.execute(insert_query)\n\n# Close the connection\ncursor.close()\nconn.close()\n<\/pre>\n\n\n\n<p>And that&#8217;s it! You should know all the steps now. The next paragraph is about a wrap up script to do all steps at once. Indeed, every step by itself will not work as you need to declare the connection before connecting to the  MS SQL table for exemple.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"ovGCtcKaqb\"><a href=\"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/\">How to import an XML document into SQL Server using Python?<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;How to import an XML document into SQL Server using Python?&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/python\/import-xml-document-in-sql-server-python\/embed\/#?secret=fuzrOxKscX#?secret=ovGCtcKaqb\" data-secret=\"ovGCtcKaqb\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"wrap-up-all-the-steps-into-one-functional-script\"><\/span>Wrap up all the steps into one functional script<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import pandas as pd\nimport pyodbc\n\n# Define the path to the CSV file\ncsv_file = 'c:\\data\\employees.csv'\n\n# import the pyodbc module to manage the odbc conection\nimport pyodbc\n\n# declare some variables to store the connections details\ndriver = 'SQL Server'\nserver = 'localhost'\ndatabase = 'Expert-Only'\n\n# connect to the local SQL Server database\nconnection = pyodbc.connect(f'DRIVER={driver};'\n                            f'SERVER={server};'\n                            f'DATABASE={database};'\n                            f'Trusted_Connection=yes;')\n\ncursor = connection.cursor()\n\n# Create the SQL Server table (if it does not exist)\ncreate_table_query = \"\"\"\nCREATE TABLE employees (\n    id INT PRIMARY KEY,\n    first_name VARCHAR(50),\n    last_name VARCHAR(50),\n    email VARCHAR(50),\n    hire_date DATE\n)\n\"\"\"\n\ncursor.execute(create_table_query)\n\n# Insert the data from the CSV file into the SQL Server table\ndf = pd.read_csv(csv_file)\nfor index, row in df.iterrows():\n    insert_query = f\"\"\"\n    INSERT INTO employees (id, first_name, last_name, email, hire_date)\n    VALUES ({row['id']}, '{row['first_name']}', '{row['last_name']}', '{row['email']}', '{row['hire_date']}')\n    \"\"\"\n    cursor.execute(insert_query)\n\n# Commit the changes to the database\nconnection.commit()\n\n# Close the connection\ncursor.close()\nconnection.close()\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"conclusion-on-importing-a-csv-file-into-sql-server-using-python\"><\/span>Conclusion on importing a CSV file into SQL Server using Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To conclude, it can be a very useful skill to know how to import data from CSV to SQL Server in Python. Especially for many data analysis and reporting tasks. By automating this Python <a href=\"https:\/\/zapier.com\/blog\/robotic-process-automation\/\" target=\"_blank\" rel=\"noreferrer noopener\">process<\/a>, save yourself a lot of time and effort compared to doing it manually using Excel. Indeed, Python provides a powerful set of tools for working with databases and data files. By mastering these tools, you can greatly enhance your productivity as a data analyst or scientist.<\/p>\n\n\n\n<p>Remember, when importing data from a CSV file into SQL Server, it is important to make sure that the SQL Server table has the same structure as the CSV file, and that the data types match. It is also a good idea to check the data for errors or inconsistencies before importing it into the database.<\/p>\n\n\n\n<p>In this tutorial, we used pandas library to read the data from the CSV file and pyodbc to connect to SQL Server and execute SQL commands. We first defined the path to the CSV file, connected to the SQL Server database, created the SQL Server table. Then we inserted the data from the CSV file into the SQL Server table, and finally committed the changes to the database. All these steps are very similar to many integration programs.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"eY7MKsp7Eg\"><a href=\"https:\/\/expert-only.com\/en\/python\/text-files-in-python\/\">Manage text files in Python using read and write methods<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Manage text files in Python using read and write methods&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/python\/text-files-in-python\/embed\/#?secret=nee8EEobVH#?secret=eY7MKsp7Eg\" data-secret=\"eY7MKsp7Eg\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Step-by-step coding tutorial on how to import data from a CSV file into a SQL Server table using Python. In this tutorial, we will learn how to import data from CSV files into SQL Server using only Python scripts. <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/\" title=\"How to import data from CSV to SQL Server in Python ?\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":10744,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[657],"tags":[],"class_list":{"0":"post-23377","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.7 (Yoast SEO v26.2) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python - How to import data from CSV to SQL Server ?<\/title>\n<meta name=\"description\" content=\"In this step-by-step tutorial, learn how to import data from a CSV file into a SQL Server table using only Python programming and T-SQL.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to import data from CSV to SQL Server in Python ?\" \/>\n<meta property=\"og:description\" content=\"In this step-by-step tutorial, learn how to import data from a CSV file into a SQL Server table using only Python programming and T-SQL.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL and IT Tutorials\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ExpertOnlyCom\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-23T06:10:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-03-15T20:12:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/macro-1452987_1920x1080.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Expert-Only\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@expert_only\" \/>\n<meta name=\"twitter:site\" content=\"@expert_only\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Expert-Only\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/\"},\"author\":{\"name\":\"Expert-Only\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\"},\"headline\":\"How to import data from CSV to SQL Server in Python ?\",\"datePublished\":\"2023-01-23T06:10:00+00:00\",\"dateModified\":\"2023-03-15T20:12:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/\"},\"wordCount\":748,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/macro-1452987_1920x1080.jpg\",\"articleSection\":[\"Python tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/\",\"url\":\"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/\",\"name\":\"Python - How to import data from CSV to SQL Server ?\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/macro-1452987_1920x1080.jpg\",\"datePublished\":\"2023-01-23T06:10:00+00:00\",\"dateModified\":\"2023-03-15T20:12:58+00:00\",\"description\":\"In this step-by-step tutorial, learn how to import data from a CSV file into a SQL Server table using only Python programming and T-SQL.\",\"breadcrumb\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/#primaryimage\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/macro-1452987_1920x1080.jpg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/macro-1452987_1920x1080.jpg\",\"width\":1920,\"height\":1080},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"en\",\"item\":\"https:\/\/expert-only.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to import data from CSV to SQL Server in Python ?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/expert-only.com\/en\/#website\",\"url\":\"https:\/\/expert-only.com\/en\/\",\"name\":\"SQL and IT Tutorials\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/expert-only.com\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/expert-only.com\/en\/#organization\",\"name\":\"Expert-Only\",\"url\":\"https:\/\/expert-only.com\/en\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg\",\"width\":381,\"height\":174,\"caption\":\"Expert-Only\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/ExpertOnlyCom\/\",\"https:\/\/x.com\/expert_only\",\"https:\/\/www.youtube.com\/channel\/UCMS5sR_FwAetB0FmciNvUaA\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\",\"name\":\"Expert-Only\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g\",\"caption\":\"Expert-Only\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python - How to import data from CSV to SQL Server ?","description":"In this step-by-step tutorial, learn how to import data from a CSV file into a SQL Server table using only Python programming and T-SQL.","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:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/","og_locale":"en_US","og_type":"article","og_title":"How to import data from CSV to SQL Server in Python ?","og_description":"In this step-by-step tutorial, learn how to import data from a CSV file into a SQL Server table using only Python programming and T-SQL.","og_url":"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/","og_site_name":"SQL and IT Tutorials","article_publisher":"https:\/\/www.facebook.com\/ExpertOnlyCom\/","article_published_time":"2023-01-23T06:10:00+00:00","article_modified_time":"2023-03-15T20:12:58+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/macro-1452987_1920x1080.jpg","type":"image\/jpeg"}],"author":"Expert-Only","twitter_card":"summary_large_image","twitter_creator":"@expert_only","twitter_site":"@expert_only","twitter_misc":{"Written by":"Expert-Only","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/#article","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/"},"author":{"name":"Expert-Only","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef"},"headline":"How to import data from CSV to SQL Server in Python ?","datePublished":"2023-01-23T06:10:00+00:00","dateModified":"2023-03-15T20:12:58+00:00","mainEntityOfPage":{"@id":"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/"},"wordCount":748,"commentCount":0,"publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"image":{"@id":"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/macro-1452987_1920x1080.jpg","articleSection":["Python tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/","url":"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/","name":"Python - How to import data from CSV to SQL Server ?","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/#primaryimage"},"image":{"@id":"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/macro-1452987_1920x1080.jpg","datePublished":"2023-01-23T06:10:00+00:00","dateModified":"2023-03-15T20:12:58+00:00","description":"In this step-by-step tutorial, learn how to import data from a CSV file into a SQL Server table using only Python programming and T-SQL.","breadcrumb":{"@id":"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/#primaryimage","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/macro-1452987_1920x1080.jpg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/macro-1452987_1920x1080.jpg","width":1920,"height":1080},{"@type":"BreadcrumbList","@id":"https:\/\/expert-only.com\/en\/python\/python-import-csv-data-to-sql-server\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"en","item":"https:\/\/expert-only.com\/en\/"},{"@type":"ListItem","position":2,"name":"How to import data from CSV to SQL Server in Python ?"}]},{"@type":"WebSite","@id":"https:\/\/expert-only.com\/en\/#website","url":"https:\/\/expert-only.com\/en\/","name":"SQL and IT Tutorials","description":"","publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/expert-only.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/expert-only.com\/en\/#organization","name":"Expert-Only","url":"https:\/\/expert-only.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg","width":381,"height":174,"caption":"Expert-Only"},"image":{"@id":"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ExpertOnlyCom\/","https:\/\/x.com\/expert_only","https:\/\/www.youtube.com\/channel\/UCMS5sR_FwAetB0FmciNvUaA"]},{"@type":"Person","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef","name":"Expert-Only","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g","caption":"Expert-Only"}}]}},"_links":{"self":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/23377","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/comments?post=23377"}],"version-history":[{"count":0,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/23377\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/10744"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=23377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=23377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=23377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}