{"id":21139,"date":"2020-06-16T11:42:00","date_gmt":"2020-06-16T11:42:00","guid":{"rendered":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/http\/itsourcecode.com\/?p=21139"},"modified":"2025-03-26T02:53:55","modified_gmt":"2025-03-26T02:53:55","slug":"python-mysql-connection-simple-python-step-by-step-guide","status":"publish","type":"post","link":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/free-projects\/python-projects\/python-mysql-connection-simple-python-step-by-step-guide\/","title":{"rendered":"Python MySQL Connection: Simple Python Step by Step Guide"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"h-python-mysql-connection-simple-python-step-by-step-guide\">Python MySQL Connection: Simple Python Step by Step Guide<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Python is becoming popular to students because of its simplicity. Together with a database, you can create unique and powerful projects. This article will guide you in establishing a <strong>Python MySQL connection<\/strong> using <strong>Pycharm <\/strong>and <strong>XAMPP<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-tools\">Tools<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For this example, you will use <strong><a href=\"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/www.jetbrains.com\/pycharm\/download\/\">Pycharm<\/a> IDE<\/strong> or you other IDEs that support Python. You will also use <a href=\"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/www.apachefriends.org\/index.html\">XAMPP<\/a>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you are new to Pycharm, it will take time to install and to create a project for the first time. But after that, it will be smooth sailing. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now that is out of the way, let&#8217;s get started.<\/p>\n\n\n\n<div class=\"schema-how-to wp-block-yoast-how-to-block\"><p class=\"schema-how-to-description\"><strong>How to establish Python MySQL Connection<\/strong><\/p> <ol class=\"schema-how-to-steps\"><li class=\"schema-how-to-step\" id=\"how-to-step-1591617128749\"><strong class=\"schema-how-to-step-name\">Setting-up your project.<\/strong> <p class=\"schema-how-to-step-text\">If you used Pycharm before, you can skip this step. This first step is configuring your project. To do this, you need to click at the &#8220;<strong>Add Configuration<\/strong>&#8221; button at the upper right of Pycharm. <br\/><br\/>When the &#8220;<strong>Run\/Debug Configuration<\/strong>&#8221; window appears, click the <strong>plus icon<\/strong> and choose Python. Now you can name your configuration, choose the module name (which is the python mysql connection file) and choose your Python interpreter. There will be an interpreter already installed because Pycharm did it for you. Just select it and click OK.<br\/><br\/>You can skip this step if this is not your first time in Pycharm. You only need to do this once but you can also add other configurations for the future.<img decoding=\"async\" src=\"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/uploads\/2020\/06\/python-mysql-connection-setup.jpg\" alt=\"python mysql connection setup\"\/><\/p> <\/li><li class=\"schema-how-to-step\" id=\"how-to-step-1592026588698\"><strong class=\"schema-how-to-step-name\">Creating the database<\/strong> <p class=\"schema-how-to-step-text\">Run XAMPP and create your database. For this example, I have a database called <strong>python_db<\/strong> with a <strong>students<\/strong> table. this table has 4 fields: <strong>id, fName, mName, and lName<\/strong>. <\/p> <\/li><li class=\"schema-how-to-step\" id=\"how-to-step-1591777923244\"><strong class=\"schema-how-to-step-name\">MySQL pip command<\/strong> <p class=\"schema-how-to-step-text\">Installing the python mysql connection connector is easy using the pip command. In the Pycharm terminal, just type the command <strong>pip3 install mysql-connector<\/strong>. This will download the connector for our database. <\/p> <\/li><li class=\"schema-how-to-step\" id=\"how-to-step-1592006694466\"><strong class=\"schema-how-to-step-name\">The actual code<\/strong> <p class=\"schema-how-to-step-text\">Now you are ready to code! First is to import the mysql connector using <strong>import mysql.connector<\/strong>. Then create a variable for the mysql connector and set its parameters with <strong>mysql.connector.connect()<\/strong>. <br\/><br\/>These parameters are your usual database parameters like username, host, password, and database name. After that, you will create <strong>dbConnection.cursor()<\/strong> that will hold the data. Next is your SQL and your for loop to display the data. Below is the screenshot of a sample code.<img decoding=\"async\" src=\"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/uploads\/2020\/06\/python-mysql-connection-code.jpg\" alt=\"python mysql connection code\"\/><\/p> <\/li><li class=\"schema-how-to-step\" id=\"how-to-step-1592044422913\"><strong class=\"schema-how-to-step-name\">The output<\/strong> <p class=\"schema-how-to-step-text\"><img loading=\"lazy\" decoding=\"async\" width=\"1280\" height=\"720\" src=\"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/uploads\/2020\/06\/python-mysql-connection-output.jpg\" class=\"attachment-full size-full\" alt=\"sample output of python mysql connection\" style=\"max-width: 100%; height: auto;\" srcset=\"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/uploads\/2020\/06\/python-mysql-connection-output.jpg 1280w, https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/uploads\/2020\/06\/python-mysql-connection-output-300x169.jpg 300w, https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/uploads\/2020\/06\/python-mysql-connection-output-1024x576.jpg 1024w, https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/uploads\/2020\/06\/python-mysql-connection-output-768x432.jpg 768w, https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/uploads\/2020\/06\/python-mysql-connection-output-696x392.jpg 696w, https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/uploads\/2020\/06\/python-mysql-connection-output-1068x601.jpg 1068w, https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/uploads\/2020\/06\/python-mysql-connection-output-747x420.jpg 747w\" sizes=\"auto, (max-width: 1280px) 100vw, 1280px\" \/><\/p> <\/li><\/ol><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">That&#8217;s how to establish a Python MySQL connection in Pycharm and XAMPP. Python is simple and easy to learn so why don&#8217;t you give it a try? For more Python articles, click the links below.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/python-projects\/python-range-function-explained\/\">Python Range Function<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/free-projects\/php-project\/connect-database-using-php-tutorial\/\">Database connection using PHP<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-inquiries\"> Inquiries<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If you have any questions or suggestions about the <strong>python mysql connection example<\/strong>, please feel free to leave a comment below.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python MySQL Connection: Simple Python Step by Step Guide Python is becoming popular to students because of its simplicity. Together with a database, you can create unique and powerful projects. &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Python MySQL Connection: Simple Python Step by Step Guide\" class=\"read-more button\" href=\"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/free-projects\/python-projects\/python-mysql-connection-simple-python-step-by-step-guide\/\" aria-label=\"Read more about Python MySQL Connection: Simple Python Step by Step Guide\">Read more<\/a><\/p>\n","protected":false},"author":2018,"featured_media":21172,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19632],"tags":[22037,130,22036,20888,22035,22230,21760,21761,21762,16],"class_list":["post-21139","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-projects","tag-how-to-connect-python-to-mysql","tag-mysql-database","tag-pycharm","tag-python-ideas","tag-python-mysql-connection","tag-python-mysql-database-connection-example","tag-python-projects","tag-python-projects-with-source-code","tag-python-source-code","tag-xampp","resize-featured-image"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.1 (Yoast SEO v27.8) - https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Python MySQL Connection: Simple Python Step by Step Guide - Itsourcecode.com<\/title>\n<meta name=\"description\" content=\"This guide will help you establish a python mysql connection using Pycharm. Using Python and MySQL in your projects is an advantage this\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/free-projects\/python-projects\/python-mysql-connection-simple-python-step-by-step-guide\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python MySQL Connection: Simple Python Step by Step Guide\" \/>\n<meta property=\"og:description\" content=\"This guide will help you establish a python mysql connection using Pycharm. Using Python and MySQL in your projects is an advantage this\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/free-projects\/python-projects\/python-mysql-connection-simple-python-step-by-step-guide\/\" \/>\n<meta property=\"og:site_name\" content=\"Itsourcecode.com\" \/>\n<meta property=\"article:published_time\" content=\"2020-06-16T11:42:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-03-26T02:53:55+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/uploads\/2020\/06\/Python-MySQL-Connection.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"John Gabriel Gestosani\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"John Gabriel Gestosani\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/free-projects\\\/python-projects\\\/python-mysql-connection-simple-python-step-by-step-guide\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/free-projects\\\/python-projects\\\/python-mysql-connection-simple-python-step-by-step-guide\\\/\"},\"author\":{\"name\":\"John Gabriel Gestosani\",\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/#\\\/schema\\\/person\\\/bcc9b7398170cce8c4fb2a3c7ca58143\"},\"headline\":\"Python MySQL Connection: Simple Python Step by Step Guide\",\"datePublished\":\"2020-06-16T11:42:00+00:00\",\"dateModified\":\"2025-03-26T02:53:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/free-projects\\\/python-projects\\\/python-mysql-connection-simple-python-step-by-step-guide\\\/\"},\"wordCount\":462,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/#\\\/schema\\\/person\\\/3883cf6bf7d0f141f81ccef0de9dd3fd\"},\"image\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/free-projects\\\/python-projects\\\/python-mysql-connection-simple-python-step-by-step-guide\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/Python-MySQL-Connection.png\",\"keywords\":[\"How to connect Python to MySQL\",\"MySQL Database\",\"PyCharm\",\"python ideas\",\"python mysql connection\",\"python mysql database connection example\",\"python projects\",\"python projects with source code\",\"python source code\",\"XAMPP\"],\"articleSection\":[\"Best Python Projects with Source Code \u2014 250+ Free Capstones for BSIT Students (2026)\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/itsourcecode.com\\\/free-projects\\\/python-projects\\\/python-mysql-connection-simple-python-step-by-step-guide\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/free-projects\\\/python-projects\\\/python-mysql-connection-simple-python-step-by-step-guide\\\/\",\"url\":\"https:\\\/\\\/itsourcecode.com\\\/free-projects\\\/python-projects\\\/python-mysql-connection-simple-python-step-by-step-guide\\\/\",\"name\":\"Python MySQL Connection: Simple Python Step by Step Guide - Itsourcecode.com\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/free-projects\\\/python-projects\\\/python-mysql-connection-simple-python-step-by-step-guide\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/free-projects\\\/python-projects\\\/python-mysql-connection-simple-python-step-by-step-guide\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/Python-MySQL-Connection.png\",\"datePublished\":\"2020-06-16T11:42:00+00:00\",\"dateModified\":\"2025-03-26T02:53:55+00:00\",\"description\":\"This guide will help you establish a python mysql connection using Pycharm. Using Python and MySQL in your projects is an advantage this\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/free-projects\\\/python-projects\\\/python-mysql-connection-simple-python-step-by-step-guide\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/itsourcecode.com\\\/free-projects\\\/python-projects\\\/python-mysql-connection-simple-python-step-by-step-guide\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/free-projects\\\/python-projects\\\/python-mysql-connection-simple-python-step-by-step-guide\\\/#primaryimage\",\"url\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/Python-MySQL-Connection.png\",\"contentUrl\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/Python-MySQL-Connection.png\",\"width\":1280,\"height\":720,\"caption\":\"Easy Step by Step Python Tutorials\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/free-projects\\\/python-projects\\\/python-mysql-connection-simple-python-step-by-step-guide\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/itsourcecode.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python MySQL Connection: Simple Python Step by Step Guide\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/#website\",\"url\":\"https:\\\/\\\/itsourcecode.com\\\/\",\"name\":\"Itsourcecode.com\",\"description\":\"Partner In Your Coding Journey!\",\"publisher\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/#\\\/schema\\\/person\\\/3883cf6bf7d0f141f81ccef0de9dd3fd\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/itsourcecode.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/#\\\/schema\\\/person\\\/3883cf6bf7d0f141f81ccef0de9dd3fd\",\"name\":\"itsourcecode\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/IT-SOURCECODE_ICON-07.jpg\",\"url\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/IT-SOURCECODE_ICON-07.jpg\",\"contentUrl\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/IT-SOURCECODE_ICON-07.jpg\",\"width\":409,\"height\":409,\"caption\":\"itsourcecode\"},\"logo\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/IT-SOURCECODE_ICON-07.jpg\"},\"description\":\"Hello Itsourcecoders, welcome to itsourcecode.com. I'm Joken Villanueva, MIT a passionate Blogger, Programmer and a Hobbyist. I started Itsourcecode because I wanted to give back and Share all the learnings and knowledge I've learned in my career and I believe through this website I would be able to help and assist those newbie programmers in enhancing their skills from different programming languages. So let us all help each other by sharing our ideas!\",\"sameAs\":[\"https:\\\/\\\/itsourcecode.com\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/#\\\/schema\\\/person\\\/bcc9b7398170cce8c4fb2a3c7ca58143\",\"name\":\"John Gabriel Gestosani\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/litespeed\\\/avatar\\\/6c0c9910a6ae352f171ef9a4f5189d4c.jpg?ver=1781968474\",\"url\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/litespeed\\\/avatar\\\/6c0c9910a6ae352f171ef9a4f5189d4c.jpg?ver=1781968474\",\"contentUrl\":\"https:\\\/\\\/itsourcecode.com\\\/wp-content\\\/litespeed\\\/avatar\\\/6c0c9910a6ae352f171ef9a4f5189d4c.jpg?ver=1781968474\",\"caption\":\"John Gabriel Gestosani\"},\"description\":\"I am an IT professional based on Negros Occidental, Philippines and currently teaching various Information Technology subjects at a state college.\",\"sameAs\":[\"John Gabriel Gestosani\"]},{\"@type\":\"HowTo\",\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/free-projects\\\/python-projects\\\/python-mysql-connection-simple-python-step-by-step-guide\\\/#howto-1\",\"name\":\"Python MySQL Connection: Simple Python Step by Step Guide\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/free-projects\\\/python-projects\\\/python-mysql-connection-simple-python-step-by-step-guide\\\/#article\"},\"description\":\"<strong>How to establish Python MySQL Connection<\\\/strong>\",\"step\":[{\"@type\":\"HowToStep\",\"url\":\"https:\\\/\\\/itsourcecode.com\\\/free-projects\\\/python-projects\\\/python-mysql-connection-simple-python-step-by-step-guide\\\/#how-to-step-1591617128749\",\"name\":\"Setting-up your project.\",\"itemListElement\":[{\"@type\":\"HowToDirection\",\"text\":\"If you used Pycharm before, you can skip this step. This first step is configuring your project. To do this, you need to click at the \\\"Add Configuration\\\" button at the upper right of Pycharm. When the \\\"Run\\\/Debug Configuration\\\" window appears, click the plus icon and choose Python. Now you can name your configuration, choose the module name (which is the python mysql connection file) and choose your Python interpreter. There will be an interpreter already installed because Pycharm did it for you. Just select it and click OK.You can skip this step if this is not your first time in Pycharm. You only need to do this once but you can also add other configurations for the future.\"}],\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/free-projects\\\/python-projects\\\/python-mysql-connection-simple-python-step-by-step-guide\\\/#schema-image-371200ea764411a29ab1e4d148e3169c\",\"url\":\"http:\\\/\\\/itsourcecode.com\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/python-mysql-connection-setup.jpg\",\"contentUrl\":\"http:\\\/\\\/itsourcecode.com\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/python-mysql-connection-setup.jpg\"}},{\"@type\":\"HowToStep\",\"url\":\"https:\\\/\\\/itsourcecode.com\\\/free-projects\\\/python-projects\\\/python-mysql-connection-simple-python-step-by-step-guide\\\/#how-to-step-1592026588698\",\"name\":\"Creating the database\",\"itemListElement\":[{\"@type\":\"HowToDirection\",\"text\":\"Run XAMPP and create your database. For this example, I have a database called python_db with a students table. this table has 4 fields: id, fName, mName, and lName.\"}]},{\"@type\":\"HowToStep\",\"url\":\"https:\\\/\\\/itsourcecode.com\\\/free-projects\\\/python-projects\\\/python-mysql-connection-simple-python-step-by-step-guide\\\/#how-to-step-1591777923244\",\"name\":\"MySQL pip command\",\"itemListElement\":[{\"@type\":\"HowToDirection\",\"text\":\"Installing the python mysql connection connector is easy using the pip command. In the Pycharm terminal, just type the command pip3 install mysql-connector. This will download the connector for our database.\"}]},{\"@type\":\"HowToStep\",\"url\":\"https:\\\/\\\/itsourcecode.com\\\/free-projects\\\/python-projects\\\/python-mysql-connection-simple-python-step-by-step-guide\\\/#how-to-step-1592006694466\",\"name\":\"The actual code\",\"itemListElement\":[{\"@type\":\"HowToDirection\",\"text\":\"Now you are ready to code! First is to import the mysql connector using import mysql.connector. Then create a variable for the mysql connector and set its parameters with mysql.connector.connect(). These parameters are your usual database parameters like username, host, password, and database name. After that, you will create dbConnection.cursor() that will hold the data. Next is your SQL and your for loop to display the data. Below is the screenshot of a sample code.\"}],\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/itsourcecode.com\\\/free-projects\\\/python-projects\\\/python-mysql-connection-simple-python-step-by-step-guide\\\/#schema-image-00147d2ed2e2828de2c60a603f7b09ff\",\"url\":\"http:\\\/\\\/itsourcecode.com\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/python-mysql-connection-code.jpg\",\"contentUrl\":\"http:\\\/\\\/itsourcecode.com\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/python-mysql-connection-code.jpg\"}},{\"@type\":\"HowToStep\",\"url\":\"https:\\\/\\\/itsourcecode.com\\\/free-projects\\\/python-projects\\\/python-mysql-connection-simple-python-step-by-step-guide\\\/#how-to-step-1592044422913\",\"text\":\"The output\"}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python MySQL Connection: Simple Python Step by Step Guide - Itsourcecode.com","description":"This guide will help you establish a python mysql connection using Pycharm. Using Python and MySQL in your projects is an advantage this","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:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/free-projects\/python-projects\/python-mysql-connection-simple-python-step-by-step-guide\/","og_locale":"en_US","og_type":"article","og_title":"Python MySQL Connection: Simple Python Step by Step Guide","og_description":"This guide will help you establish a python mysql connection using Pycharm. Using Python and MySQL in your projects is an advantage this","og_url":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/free-projects\/python-projects\/python-mysql-connection-simple-python-step-by-step-guide\/","og_site_name":"Itsourcecode.com","article_published_time":"2020-06-16T11:42:00+00:00","article_modified_time":"2025-03-26T02:53:55+00:00","og_image":[{"width":1280,"height":720,"url":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/uploads\/2020\/06\/Python-MySQL-Connection.png","type":"image\/png"}],"author":"John Gabriel Gestosani","twitter_card":"summary_large_image","twitter_misc":{"Written by":"John Gabriel Gestosani","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/free-projects\/python-projects\/python-mysql-connection-simple-python-step-by-step-guide\/#article","isPartOf":{"@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/free-projects\/python-projects\/python-mysql-connection-simple-python-step-by-step-guide\/"},"author":{"name":"John Gabriel Gestosani","@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/#\/schema\/person\/bcc9b7398170cce8c4fb2a3c7ca58143"},"headline":"Python MySQL Connection: Simple Python Step by Step Guide","datePublished":"2020-06-16T11:42:00+00:00","dateModified":"2025-03-26T02:53:55+00:00","mainEntityOfPage":{"@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/free-projects\/python-projects\/python-mysql-connection-simple-python-step-by-step-guide\/"},"wordCount":462,"commentCount":0,"publisher":{"@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/#\/schema\/person\/3883cf6bf7d0f141f81ccef0de9dd3fd"},"image":{"@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/free-projects\/python-projects\/python-mysql-connection-simple-python-step-by-step-guide\/#primaryimage"},"thumbnailUrl":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/uploads\/2020\/06\/Python-MySQL-Connection.png","keywords":["How to connect Python to MySQL","MySQL Database","PyCharm","python ideas","python mysql connection","python mysql database connection example","python projects","python projects with source code","python source code","XAMPP"],"articleSection":["Best Python Projects with Source Code \u2014 250+ Free Capstones for BSIT Students (2026)"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/free-projects\/python-projects\/python-mysql-connection-simple-python-step-by-step-guide\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/free-projects\/python-projects\/python-mysql-connection-simple-python-step-by-step-guide\/","url":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/free-projects\/python-projects\/python-mysql-connection-simple-python-step-by-step-guide\/","name":"Python MySQL Connection: Simple Python Step by Step Guide - Itsourcecode.com","isPartOf":{"@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/free-projects\/python-projects\/python-mysql-connection-simple-python-step-by-step-guide\/#primaryimage"},"image":{"@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/free-projects\/python-projects\/python-mysql-connection-simple-python-step-by-step-guide\/#primaryimage"},"thumbnailUrl":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/uploads\/2020\/06\/Python-MySQL-Connection.png","datePublished":"2020-06-16T11:42:00+00:00","dateModified":"2025-03-26T02:53:55+00:00","description":"This guide will help you establish a python mysql connection using Pycharm. Using Python and MySQL in your projects is an advantage this","breadcrumb":{"@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/free-projects\/python-projects\/python-mysql-connection-simple-python-step-by-step-guide\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/free-projects\/python-projects\/python-mysql-connection-simple-python-step-by-step-guide\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/free-projects\/python-projects\/python-mysql-connection-simple-python-step-by-step-guide\/#primaryimage","url":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/uploads\/2020\/06\/Python-MySQL-Connection.png","contentUrl":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/uploads\/2020\/06\/Python-MySQL-Connection.png","width":1280,"height":720,"caption":"Easy Step by Step Python Tutorials"},{"@type":"BreadcrumbList","@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/free-projects\/python-projects\/python-mysql-connection-simple-python-step-by-step-guide\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/"},{"@type":"ListItem","position":2,"name":"Python MySQL Connection: Simple Python Step by Step Guide"}]},{"@type":"WebSite","@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/#website","url":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/","name":"Itsourcecode.com","description":"Partner In Your Coding Journey!","publisher":{"@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/#\/schema\/person\/3883cf6bf7d0f141f81ccef0de9dd3fd"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/#\/schema\/person\/3883cf6bf7d0f141f81ccef0de9dd3fd","name":"itsourcecode","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/uploads\/2021\/01\/IT-SOURCECODE_ICON-07.jpg","url":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/uploads\/2021\/01\/IT-SOURCECODE_ICON-07.jpg","contentUrl":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/uploads\/2021\/01\/IT-SOURCECODE_ICON-07.jpg","width":409,"height":409,"caption":"itsourcecode"},"logo":{"@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/uploads\/2021\/01\/IT-SOURCECODE_ICON-07.jpg"},"description":"Hello Itsourcecoders, welcome to itsourcecode.com. I'm Joken Villanueva, MIT a passionate Blogger, Programmer and a Hobbyist. I started Itsourcecode because I wanted to give back and Share all the learnings and knowledge I've learned in my career and I believe through this website I would be able to help and assist those newbie programmers in enhancing their skills from different programming languages. So let us all help each other by sharing our ideas!","sameAs":["https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/"]},{"@type":"Person","@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/#\/schema\/person\/bcc9b7398170cce8c4fb2a3c7ca58143","name":"John Gabriel Gestosani","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/litespeed\/avatar\/6c0c9910a6ae352f171ef9a4f5189d4c.jpg?ver=1781968474","url":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/litespeed\/avatar\/6c0c9910a6ae352f171ef9a4f5189d4c.jpg?ver=1781968474","contentUrl":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-content\/litespeed\/avatar\/6c0c9910a6ae352f171ef9a4f5189d4c.jpg?ver=1781968474","caption":"John Gabriel Gestosani"},"description":"I am an IT professional based on Negros Occidental, Philippines and currently teaching various Information Technology subjects at a state college.","sameAs":["John Gabriel Gestosani"]},{"@type":"HowTo","@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/free-projects\/python-projects\/python-mysql-connection-simple-python-step-by-step-guide\/#howto-1","name":"Python MySQL Connection: Simple Python Step by Step Guide","mainEntityOfPage":{"@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/free-projects\/python-projects\/python-mysql-connection-simple-python-step-by-step-guide\/#article"},"description":"<strong>How to establish Python MySQL Connection<\/strong>","step":[{"@type":"HowToStep","url":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/free-projects\/python-projects\/python-mysql-connection-simple-python-step-by-step-guide\/#how-to-step-1591617128749","name":"Setting-up your project.","itemListElement":[{"@type":"HowToDirection","text":"If you used Pycharm before, you can skip this step. This first step is configuring your project. To do this, you need to click at the \"Add Configuration\" button at the upper right of Pycharm. When the \"Run\/Debug Configuration\" window appears, click the plus icon and choose Python. Now you can name your configuration, choose the module name (which is the python mysql connection file) and choose your Python interpreter. There will be an interpreter already installed because Pycharm did it for you. Just select it and click OK.You can skip this step if this is not your first time in Pycharm. You only need to do this once but you can also add other configurations for the future."}],"image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/free-projects\/python-projects\/python-mysql-connection-simple-python-step-by-step-guide\/#schema-image-371200ea764411a29ab1e4d148e3169c","url":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/http\/itsourcecode.com\/wp-content\/uploads\/2020\/06\/python-mysql-connection-setup.jpg","contentUrl":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/http\/itsourcecode.com\/wp-content\/uploads\/2020\/06\/python-mysql-connection-setup.jpg"}},{"@type":"HowToStep","url":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/free-projects\/python-projects\/python-mysql-connection-simple-python-step-by-step-guide\/#how-to-step-1592026588698","name":"Creating the database","itemListElement":[{"@type":"HowToDirection","text":"Run XAMPP and create your database. For this example, I have a database called python_db with a students table. this table has 4 fields: id, fName, mName, and lName."}]},{"@type":"HowToStep","url":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/free-projects\/python-projects\/python-mysql-connection-simple-python-step-by-step-guide\/#how-to-step-1591777923244","name":"MySQL pip command","itemListElement":[{"@type":"HowToDirection","text":"Installing the python mysql connection connector is easy using the pip command. In the Pycharm terminal, just type the command pip3 install mysql-connector. This will download the connector for our database."}]},{"@type":"HowToStep","url":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/free-projects\/python-projects\/python-mysql-connection-simple-python-step-by-step-guide\/#how-to-step-1592006694466","name":"The actual code","itemListElement":[{"@type":"HowToDirection","text":"Now you are ready to code! First is to import the mysql connector using import mysql.connector. Then create a variable for the mysql connector and set its parameters with mysql.connector.connect(). These parameters are your usual database parameters like username, host, password, and database name. After that, you will create dbConnection.cursor() that will hold the data. Next is your SQL and your for loop to display the data. Below is the screenshot of a sample code."}],"image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/free-projects\/python-projects\/python-mysql-connection-simple-python-step-by-step-guide\/#schema-image-00147d2ed2e2828de2c60a603f7b09ff","url":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/http\/itsourcecode.com\/wp-content\/uploads\/2020\/06\/python-mysql-connection-code.jpg","contentUrl":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/http\/itsourcecode.com\/wp-content\/uploads\/2020\/06\/python-mysql-connection-code.jpg"}},{"@type":"HowToStep","url":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/free-projects\/python-projects\/python-mysql-connection-simple-python-step-by-step-guide\/#how-to-step-1592044422913","text":"The output"}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-json\/wp\/v2\/posts\/21139","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-json\/wp\/v2\/users\/2018"}],"replies":[{"embeddable":true,"href":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-json\/wp\/v2\/comments?post=21139"}],"version-history":[{"count":3,"href":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-json\/wp\/v2\/posts\/21139\/revisions"}],"predecessor-version":[{"id":126331,"href":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-json\/wp\/v2\/posts\/21139\/revisions\/126331"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-json\/wp\/v2\/media\/21172"}],"wp:attachment":[{"href":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-json\/wp\/v2\/media?parent=21139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-json\/wp\/v2\/categories?post=21139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/itsourcecode.com\/wp-json\/wp\/v2\/tags?post=21139"}],"curies":[{"name":"wp","href":"https:\/\/blue-sea-697d.quartiers047.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}